+ 1

How can i insert an input with decorator?

Hi! I have this code below and want to create an input with other function(just to understand better how decorators work). I tried my best, but i couldn`t understand how to call a variables from main function. Is there any possible solution for this problem? def imput(func): def wrap(a,b): a=input('define a: ') b=input('define b: ') return wrap(a,b) def smart_divide(func): def inner(a,b): print("I am going to divide",a,"and",b) if b == 0: print("Whoops! cannot divide") return return func(a,b) return inner def printer(func): def innner(a,b): print('the answer is',func(a,b)) return innner @imput @smart_divide @printer def divide(a,b): s=a/b return s divide()

24th Jan 2019, 11:53 AM
Илья Ковалев
Илья Ковалев - avatar
2 Respuestas
+ 2
Hi I am not sure how it would be possible in this case to chain decorators with different functionality, but I could implement the input, the error handling and the printing in a single decorator like this: https://code.sololearn.com/c1dljvHQh70o/?ref=app
25th Jan 2019, 6:45 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thx a lot! Now I got it!
31st Jan 2019, 2:53 PM
Илья Ковалев
Илья Ковалев - avatar