Simple decorator
Decorating the function flower
, to print red roses
def decorate(func):
def _wrapper():
return "red %s" % func()
return _wrapper
@decorate
def flower():
return "roses"
print(flower)
# <function decorate.<locals>._wrapper at 0x7fdcd850c700>
print(flower())
# red roses
Decorate a function with parameters
Decorating the function flowers, which receive the flower name as a parameter.
def decorate(func):
def _wrapper(name):
return "red %s" % func(name)
return _wrapper
@decorate
def flower(name):
return name
print(flower)
# <function decorate.<locals>._wrapper at 0x7fdcd850c700>
print(flower("dahlias"))
# red dahlias
Passing data to the decorator function
The color
will be available in the inner scopes when creating the decorator .
def create_decorator(color):
def _decorate(func):
def _wrapper(name):
return f"{color} {func(name)}"
return _wrapper
return _decorate
@create_decorator("blue")
def flower(name):
return name
print(flower)
# <function create_decorator.<locals>._decorate.<locals>._wrapper at 0x7f360530c790>
print(flower("bluebell"))
# blue bluebell