0

Why the "I will be decorated" is not printed?

#Lets make a Python program def my_decorator(func): def inside(): print("Done") return func return inside @my_decorator def victim(): print("I will be decorated!") victim()

7th Apr 2020, 10:02 AM
Angelos Karasavvidis
Angelos Karasavvidis - avatar
2 Respuestas
+ 2
The line after a return function is not executed, so, 'return inside' won't run cos of the 'return func',that's why it only prints 'Done'. It should be def my_decorator(func): def inside(): print("Done") func() return inside @my_decorator def victim(): print("I will be decorated!") victim()
7th Apr 2020, 10:53 AM
Justus
Justus - avatar
+ 2
Justus The 'return func' was actually part of the 'inside' part so it didn't really have any effect on whether 'return inside' was executed or not. The only problem was that he failed to call 'func()' as you correctly pointed out.
7th Apr 2020, 11:57 AM
Russ
Russ - avatar