I am learning Python on Sololearn and I came across this bit of code which intends to decorate a function:
def decor(func):
def wrap():
print("============")
func()
print("============")
return wrap
def print_text():
print("Hello world!")
When I call it like this decor(print_text)
I just get this output <function __main__.decor.<locals>.wrap()>
To get the expected, decorated output, I have to call the function with an extra set of empty parentheses, like this decor(print_text)()
Which outputs the correct result:
============
Hello world!
============
I understand that there is an easier way to decorate without this mess, however, I am in it for the long run and I would like to get familiar with how python "thinks". So my question is, why are the extra set of parentheses () required to get the correct output? Why wont just using decor(print_text)
give the correct result?
Thanks everyone! I apologize for the long questions but I am trying to explain my thought the best I can :)
Okay y'all I think all my questions regarding this code were answered by the awesome people below. Thanks everyone for your help!