aprotyas / aprotyas.github.io

Personal website
https://aprotyas.github.io
MIT License
3 stars 0 forks source link

Blog post about decorators in Python #31

Open aprotyas opened 2 years ago

aprotyas commented 2 years ago

Helps us make code less DRY, and is often useful for auxiliary instrumentation (timing, logging, caching, etc.)

Main thoughts:

def logger(func):
    def wrapper(*args, **kwargs):
        print('-- start logger --')
        func(*args, **kwargs)
        print('-- end logger --')
    return wrapper

@logger
def foo():
    print('foo')

Reference:

Food for thought: