+ 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());

6th Feb 2021, 3:11 PM
CHANDAN ROY
CHANDAN ROY - avatar
17 Respuestas
+ 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("***")
6th Feb 2021, 3:33 PM
Abhay
Abhay - avatar
+ 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/
6th Feb 2021, 3:54 PM
David Ashton
David Ashton - avatar
+ 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();
2nd Jan 2022, 4:29 PM
Rafique
Rafique - avatar
+ 2
CHANDAN ROY works fine for me , can you link your edited code here?
6th Feb 2021, 3:43 PM
Abhay
Abhay - avatar
+ 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();
6th Feb 2021, 3:46 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 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)
6th Feb 2021, 3:56 PM
Abhay
Abhay - avatar
+ 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());
27th May 2021, 5:17 AM
Abhishek Biswas
Abhishek Biswas - avatar
+ 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. :-(
8th Jan 2022, 7:11 AM
Rafique
Rafique - avatar
+ 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?
26th Aug 2021, 8:32 PM
Anthony Amabile
Anthony Amabile - avatar
+ 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)
8th Sep 2021, 2:00 PM
Manuel Carlos Cabanillas
Manuel Carlos Cabanillas - avatar
+ 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!!
2nd Jan 2022, 4:57 PM
CHANDAN ROY
CHANDAN ROY - avatar
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());
14th Mar 2022, 7:18 PM
Abraham Callahan
Abraham Callahan - avatar
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())
18th Aug 2022, 10:34 AM
Abdul Ghafar
Abdul Ghafar - avatar
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())
10th Nov 2022, 8:28 AM
Pooja Patel
Pooja Patel - avatar
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());
25th Mar 2023, 10:23 PM
المهندس أحمد
المهندس أحمد - avatar
0
def decor(func): def wrap(*args): func(*args) return wrap @decor def invoice(num): print( num.upper()) invoice(input())
27th Feb 2024, 4:12 PM
Monisha D
Monisha D - avatar
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())
8th Apr 2024, 10:00 AM
Rashmi Kulkarni