Moving turtle after mouse click [duplicate] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this function:
def a(one, two, the_argument_function):
if one in two:
return the_argument_function
my the_argument_function looks something like this:
def b(do_this, do_that):
print "hi."
Both of the above are imported to a file "main_functions.py" for my ultimate code to look like this:
print function_from_main(package1.a, argument, package2.b(do_this, do_that)
The "if one in two" from "a"function works but "b"function still executes when being passed to "function_from_main" without waiting the check from "a" to see if it actually should execute.
What can I do?

package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a
You will also need to modify function a such that function be is called when the condition is satisfied
# function a definition
def a(one, two, the_argument_function, argument_dict):
if one in two:
return the_argument_function(**argument_dict)
def b(do_this, do_that):
print "hi."
# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value})

Related

In Python, how no explicitly return function changes the data in another function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Python Code is here:
In the parse_subject function call, the first tuple passed in the word_list has 'stop' word_type.
I passed this list to the function parse_subject. Then within this function, after the execution of skip function I find word_list length shortened as that very tuple no longer exists within the word_list.
So the word_list available to the called peek function is this new word_list.
Consider that match function return values to skip function. But skip function does not return anything explicitly.
I want to ask that when skip function does not return anything explicitly, then how the word_list value in the parse_subject gets updated after the execution of skip function as we could see that the updated word_list then is further used in the called peek function within the parse_subject function.
This is because the following line in match function
word = word_list.pop(0)
please note that python passes objects by reference, this is what causes the original list to be modified as well.
More info about Pop method

Invoking python functions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Trying to invoke the pay. I am doing something wrong on the "computepay()" invocation?
hrs = input("Enter Hours:")
hours = float(hrs)
def computepay():
p = computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
computepay()
pay = float(p)
print("Pay",pay)
Your overall code logic doesn't make sense. Your function declaration and definition - the way you call the function inside the function don't match.
def computepay():
The above code says computepay() doesn't take any argument.
p = computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
In this line of code, you are trying to call the function itself, but with arguments which it doesn't accept and hence you are getting the error.
Apart from this, neither does the argument that you are passing on that line makes much sense. You have called computepay() with argument - (hours>=40*10.5) + (hours>40*(10.5*1.5) but that won't work. (hours>=40*10.5) and (hours>40*(10.5*1.5) will evaluate to booleans and you can't add two boolean values.
I don't know what the logic is that you are trying to implement, but these are some major and obvious faults in your code that I could find.
def computepay():
This function does not accept any parameters and has not been overloaded in any point of the code, so
computepay((hours>=40*10.5) + (hours>40*(10.5*1.5))
will not work. You can overload it in another section of the code as:
def computepay(argument):
but unless you do it so, it will never work. In addition
(hours>=40*10.5)
this is a boolean value meaning that it is either true or false. You are trying to sum 2 boolean values, that also does not make sense. As far as I can see you are trying to implement recursion, hence your function invokes an instance of itself (or another unimplemented overloaded version) that will also not work because you do not have a base case. For more information: https://www.youtube.com/watch?v=nxiObVwQ8MM

Understanding Python decorators [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I wrote a small piece of code to run it myself and understand the flow:
def perform_arithmetic_ops(func):
print("INSIDE PERFORM ARTITHMETIC")
def decorate(*args):
print("I JUST GOT DECORATED")
print(f"SUM -> {args[0]+args[1]}")
func(args[0],args[1])
return decorate
#perform_arithmetic_ops
def print_me(x,y):
print(f"X = {x} ; Y = {y}")
print_me(5,4)
Please correct me if my understanding, which I've summarized in the form points below, is incorrect.
def perform_arithmetic_ops is the decorator which only takes a callable object, in this case a function as its argument, correct?
The inner function def decorate() here is what actually performs the desired modification. What I noticed was commenting func(args[0],args[1]) inside def decorate() basically meant that the X={x} ; Y={y} statement wasn't being printed. So does this mean that the the decorator replaces everything the original function does? If yes, how is that a modification? Isn't it more of a replacement for another function?
return decorate() i.e. calling decorate() instead of returning it led to errors for tuple index out of range. Why?
Take a good look at this enormous answer/novel. It's one of the best explanations I've come across.
def perform_arithmetic_ops is the decorator which only takes a callable object, in this case a function as its argument, correct?
Yes. You are right about it.
The inner function def decorate() here is what actually performs the desired modification. What I noticed was commenting func(args[0],args[1]) inside def decorate() basically meant that the X={x} ; Y={y} statement wasn't being printed. So does this mean that the the decorator replaces everything the original function does? If yes, how is that a modification? Isn't it more of a replacement for another function?
A decorator is a function that takes a function as its only parameter and returns a function. This is helpful to “wrap” functionality with the same code over and over again. It is wrapping but not modification.
return decorate() i.e. calling decorate() instead of returning it led to errors for tuple index out of range. Why?
To understand decorators, you must first understand that functions are objects in Python.
Because you might be calling it without any parameters. Try this return decorate(5,1). But after this statement and if you try to decorate function like print_me, then print_me might be holding non-callable object. See this:
def perform_arithmetic_ops(func):
print("INSIDE PERFORM ARTITHMETIC")
def decorate(*args):
print("I JUST GOT DECORATED")
print(f"SUM -> {args[0]+args[1]}")
return func(args[0], args[1])
return decorate(5, 1)
#perform_arithmetic_ops
def print_me(x,y):
print(f"X = {x} ; Y = {y}")
return 5
# print_me(5,2) # Will throw: TypeError: 'int' object is not callable
print(print_me)

How to pass function as argument without executing it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this function:
def a(one, two, the_argument_function):
if one in two:
return the_argument_function
my the_argument_function looks something like this:
def b(do_this, do_that):
print "hi."
Both of the above are imported to a file "main_functions.py" for my ultimate code to look like this:
print function_from_main(package1.a, argument, package2.b(do_this, do_that)
The "if one in two" from "a"function works but "b"function still executes when being passed to "function_from_main" without waiting the check from "a" to see if it actually should execute.
What can I do?
package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a
You will also need to modify function a such that function be is called when the condition is satisfied
# function a definition
def a(one, two, the_argument_function, argument_dict):
if one in two:
return the_argument_function(**argument_dict)
def b(do_this, do_that):
print "hi."
# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value})

python .apply calling function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
def awesome_count():
if 'awesome' in dict:
return 1
else:
return 0
products['awesome']= products['word_count'].apply(awesome_count)
TypeError: awesome_count() takes no arguments (1 given)
what is the issue with calling the function. can somebody help?
Looks like your awesome_count function should take one argument, dict:
def awesome_count(dict):
....
You should really call it something besides dict though as that is a built-in data type. For something simple like this d would be fine.
Another thing to keep in mind is that Python is not javascript -- there is no apply method on dicts unless you have subclassed and added it yourself.

Categories