Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

examples of function decorators in Python

from functools import wraps

def logit(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)
    return with_logging

@logit
def addition_func(x):
   """Do some math."""
   return x + x


result = addition_func(4)
# Output: addition_func was called
Source by book.pythontips.com #
 
PREVIOUS NEXT
Tagged: #examples #function #decorators #Python
ADD COMMENT
Topic
Name
2+8 =