28
loading...
This website collects cookies to deliver better user experience
def say_hello():
return "hello"
print(say_hello())
print(say_hello)
hello
<function say_hello at 0x000001BA3891D1F0>
def say_hello_decorator(func):
return func
def say_hello():
return 'Hello '
say_hello = say_hello_decorator(say_hello)
print(say_hello())
say_hello = say_hello_decorator(say_hello)
. When this block runs, python assigns the address of the function object, which is the value of say_hello, to another variable named 'func', which is the local variable in the function. So now func = . Afterwards, when the say_hello_decorator function runs, it returns the address it keeps in the func and assigns the same address to the say_hello variable. When we run it, the say_hello function starts to run and we get the 'Hello' output.def say_hello_decorator(func):
return func
@say_hello_decorator #say_hello = say_hello_decorator(say_hello)
def say_hello():
return 'Hello '
print(say_hello())
def say_hello_decorator(func):
def wrapper():
return func
return wrapper
@say_hello_decorator
def say_hello():
return 'Hello '
print(say_hello())
def say_hello_decorator(func):
def wrapper(name):
val = func(name)
print("Wrapper works")
return val
return wrapper
@say_hello_decorator
def say_hello(name):
return 'Hello ' + name
print(say_hello("Berkin Öztürk"))