0
Here in give below pgm why we called varable decorated instead of print it
def decor(func): def wrap(): print("============") func() print("============") return wrap def print_text(): print("Hello world!") decorated = decor(print_text) decorated ()
1 ответ
0
You can print a variable. decorated is a variable that contains a method (decor(func), where func=print_text). So, it is callable:
>>> type(decorated)
<class 'function'>
>>> print(decorated)
<function wrap at 0x02EDCDF8>
>>> decorated() # the same as decor(print_text)
============
Hello world!
============
Methods like this are called decorators. You can also use it like this:
@decor
def print_text():
print("Hello world!")
>>> print_text()
============
Hello world!
============