Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python decorator

def uppercase_decorator(func):
    def function_wrapper(x):
        print("Before calling " + func.__name__)
        # function that is decorated making name parameter always uppercase
        func(x.upper())
        print("After calling " + func.__name__)
    return function_wrapper

@uppercase_decorator
def user(name):
    print(f"Hi, {name}")

user("Sam")

# output
# Before calling user
# Hi, SAM
# After calling user
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #decorator
ADD COMMENT
Topic
Name
6+2 =