装饰器工作python

代码示例

22
0

python装饰器

# decorators are user to decorate functions like what to do before/after calling the function
import time

def delay_decorator(function):
    def wrapper_function():
        print("-----------------------I am gonna greet you--------------------------")
        time.sleep(2)# waits for 2 seconds
        function()   # calls the function
        time.sleep(1)# waits for 1 sec
        print("------------------How do you feel about that greet?-------------------")
    return wrapper_function

@delay_decorator
def greet():
    print("Helllllloooooooooo")

greet()
12
0

装饰器python

def our_decorator(func):
    def function_wrapper(x):
        print("Before calling " + func.__name__)
        func(x)
        print("After calling " + func.__name__)
    return function_wrapper

@our_decorator
def foo(x):
    print("Hi, foo has been called with " + str(x))

foo("Hi")

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................