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)
Related
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})
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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
def function(x+4):
return (x*5)+3
SyntaxError: invalid syntax
Why is that? i don't get the logic behind it?
(i know that it is a simple question but i couldn't find an answer to it.
i got many unclear feedbacks about my question,i am sorry about that as english is not my first language i couldn't clarify my question in short:
While i was learning about what return does,i compromised the logic with the logic behind f(x)= codomain as def function(x) = return codomain
but as you know there are functions in maths like f(x+2) = 5x+3. I thought maybe i could do the same on def function,the problem was it was giving a syntax error ,and i was curious about the design idea behind it and if there was an alternative solution to implement this in code.Thanks for answers!
This is a rather uncommon syntax you are suggesting and it is simply not part of Python's syntax, or any language that I know.
Although, what you want to do boils down to updating an argument before it is passed to a function. This can be done with a decorator.
def add_four_to_argument(f):
return lambda x: f(x + 4)
#add_four_to_argument
def func(x):
return (x*5)+3
print(func(1)) # 28
Since you asked about the logic behind that design decision, how would you expect that syntax to behave? Note that it is not the same x being shared everywhere. The following are the same:
x = 5
def f(x):
return x
print(f(x)) # Prints 5
x = 5
def f(y):
return y
print(f(x)) # Prints 5
This allows you to scope your variables and not have to debug the confusing shared state that exclusive use of global variables can cause.
Back to your question though, nowhere else in Python does the addition operator cause assignment (barring abuse of dunder methods...don't do that). So, in your hypothetical f(x+4), what happens to the x+4? Is that supposed to be a placeholder for x=x+4, or is that supposed to be assigned to some new variable representing some ethereal notion of "argument 1" for the function?
Let's dive into it a little further. What about the following example:
def f(x+y):
return x
Suddenly things got a whole lot more ambiguous. How do we call this function? Do we use f(1,2)? Do we only pass a single argument f(3) and guess which portion corresponds to x and which corresponds to y? Even if we figure out the semantics for calling such a function, what does the statement x+y actually do? Does it stand for x=x+y? For y=x+y? For arg1=x+y?
By not allowing the behavior you're describing, Python makes your intent clear. To do a thing, you have to tell Python to actually do that thing. From the Zen of Python,
explicit is better than implicit.
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})
Thanks for reading my question. As I'm still new to Python, I would like to ask about the () in Python.
def addOne(myFunc):
def addOneInside():
return myFunc() + 1
return addOneInside # <-----here is the question
#addOne
def oldFunc():
return 3
print oldFunc()
Please note that on line four, although the programme returns a function, it does not need parentheses(). Why does it NOT turn out with an error for syntax error? Thank you very much for your answers in advance!
The parentheses are used to run a function, but without them the name still refers to the function just like a variable.
return myFunc() + 1
This will evaluate the myFunc function, add 1 to its value and then return that value. The brackets are needed in order to get the function to run and return a numeric value.
return addOneInside
This is not actually running addOneInside, it is merely returning the function as a variable. You could assign this to another name and store it for later use. You could theoretically do this:
plusOne = addOneInside
plusOne()
And it will actually call the addOneInside function.
The particular instance in your initial question is known as a Decorator, and it's a way for you to perform code on the parameters being passed to your function. Your example is not very practical, but I can modify it to show a simple use case.
Let's say that you want to only have positive numbers passed to your function. If myFunc is passed a negative number, you want it to be changed to 0. You can manage this with a decorator like this.
def addOne(myFunc):
def addOneInside(num):
if num < 0:
num = 0
return myFunc(num)
return addOneInside # <-----here is the question
#addOne
def oldFunc(number):
return number
To explain, the #addOne is the decorator syntax, and it's attaching the addOneInside function to be called on the argument/s of oldFunc whenever you call it. So now here's some sample output:
oldFunc(-12)
>>> 0
oldFunc(12)
>>> 12
So now you could add logic to oldFunc that operates independently of the parameter parsing logic. You could also relatively easily change what parameters are permitted. Maybe there's also a maximum cap to hit, or you want it to log or note that the value shouldn't be negative. You can also apply this decorator to multiple functions and it will perform the same on all of them.
This blogpost explained a lot for me, so if this information is too brief to be clear, try reading the long detailed explanation there.
Your indentation in function addOne() was incorrect (I have fixed it), but I don't think that this was your problem.
If you are using Python3, then print is a function and must be called like this:
print(oldFunc())