+ 4
Python Decorator function generating error
The following code generating an error that reads "TypeError: wrap() takes 0 positional arguments". Can you please explain the reason and the possible solution to this, please. '''OUTPUT FORMAT *** INVOICE #128 *** END OF PAGE ''' #your code goes here def decor(func): def wrap(): print("***") func() print("***") print("END OF PAGE") return wrap @decor def invoice(num): print("INVOICE #" +num) invoice(input());
17 Antworten
+ 7
CHANDAN ROY you need to provide argument to function which is being passed to decorator . And that you can do by ,
def wrap(arg) :
print("***")
func(arg)
print("***")
+ 16
Try this
def decor(func):
def wrap(*args):
print("***")
func(*args)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" + num)
invoice(input())
See this https://realpython.com/primer-on-JUMP_LINK__&&__python__&&__JUMP_LINK-decorators/
+ 3
There is also a crazy shortcut cheating solution by simply using print command and forgetting deocorators and wraps, which worked fine for me! Try the code for the code coach query at https://www.sololearn.com/learning/1073
def print_bill():
print("###")
print("BILL DATA GOES HERE")
print("###")
print_bill();
+ 2
CHANDAN ROY works fine for me , can you link your edited code here?
+ 2
Abhay bro, Your solution is correct but can you please explain the way it works for me to understand.
In the lesson, it's written this way and no arguments needed inside wrap function.
I am a bit confused here. Please explain it a bit
def decor(func):
def wrap():
print("============")
func()
print("============")
return wrap
@decor
def print_text():
print("Hello world!")
print_text();
+ 2
CHANDAN ROY they might teach about that argument thing later i guess or you can go through other resources as you know sololearn just covers the basics .
If we remove the @decor keyword , then you can see how it really works ,
invoice=decor(invoice)
// invoice now holds wrap function which has our function nested inside it .
So in order to pass argument to our original function we need to pass argument to that wrapper function first so our original function can access it inside and we can do that now by calling following command,
invoice(arg)
+ 2
Add the arg as argument in warp function which is defined inside decor function which is requird for decording our OUTPUT of the program
Here is the final code
def decor(func):
def wrap(arg):
print("***")
func(arg)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" +num)
invoice(input());
+ 2
CHANDAN ROY
Absolutely, Impossible to disagree even with a word of what you have written!
I am just the wicked ole fella with a leak in his memory, who has forgotten one course, when he does another!
Will surely read up decorators once more, as it seems like deja vu to me, now. :-(
+ 1
Thanks for the answer Abhay. I don't understand that though, because the previous example in the lesson was like this:
def decor(func):
def wrap():
print("============")
func()
print("============")
return wrap
@decor
def print_text():
print("Hello world!")
print_text();
What am I missing that the example doesn't need an 'arg' and the practice problem does?
+ 1
The problem needs an argument and the lesson doesn't because:
the function in the problem (def invoice) has an argument (the invoice number) while the function in the lesson (print text) doesn't have any argument (as Hello world is inside the function, it is not an input)
+ 1
Rafique Sir, cool but the purpose of this post was to understand decorator function and the way python executes it.
I just realised it's been almost a year when I asked this question. I think I should review all the questions I asked and write a nice elaborate response on all of them for others to understand. Thank you for posting here. I got a cool mission!!
0
@David Ashton, @Abhishek Biswas, @Anthony Amabile, & @Manuel Carlos Cabanillas:
Thank you all, I think I understand now why there has to be an argument in the def wrap() and the func().
Please correct me if I am wrong: If there is more than one function(argument) in a program, the arguments of all functions must be named. If there is only one function, the argument does not need to be stated.
def decor(func):
def wrap(args):
print("***")
func(args)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" +num)
invoice(input());
0
#def decor(func):
# def wrap(num):
# print("***")
# func(num)
# print("***")
# print("END OF PAGE")
# return wrap
#@decor
#def invoice(num):
# print("INVOICE #" +num)
#invoice(input())
0
#your code goes here
def decor(func):
def wrap(*args):
print("***")
func(*args)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" + num)
invoice(input())
0
def decor(func):
def wrap(num):
print("***")
func(num)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" +num)
invoice(input());
0
def decor(func):
def wrap(*args):
func(*args)
return wrap
@decor
def invoice(num):
print( num.upper())
invoice(input())
0
def decor(func):
def wrap(num):
print("***")
func(num)
print("***")
print("END OF PAGE")
return wrap
@decor
def invoice(num):
print("INVOICE #" + num)
invoice(input())