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

21st Nov 2016, 11:29 AM
Onkar Ashok Gumate
Onkar Ashok Gumate - avatar
1 Odpowiedź
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! ============
21st Nov 2016, 5:07 PM
donkeyhot
donkeyhot - avatar