Skip to content

Instantly share code, notes, and snippets.

@tommypratama
Created June 30, 2019 18:36
Show Gist options
  • Save tommypratama/50e5274e85d499acfdf3a10a1b359647 to your computer and use it in GitHub Desktop.
Save tommypratama/50e5274e85d499acfdf3a10a1b359647 to your computer and use it in GitHub Desktop.

Python Decorators

Sometimes you can just write a function or method or class and be done with it. But sometimes you find that you need to wrap your functions or methods or classes in some sort of new behavior. And since you're a good programmer, you don't want to change every function in the same way over and over again. That's not DRY or a good use of your time! Well, when that happens, you just might need a decorator.

Example

Nested function:

def outer():
    number = 5

    def inner():
        print(number)
    inner()

outer()  # prints 5

First-class functions

def apply(func, x, y):
    return func(x, y)

def add(a, b):
    return a + b

print(apply(add, 5, 5))  # prints 10

Closures

def close():
    x = 5

    def inner():
        print(x)
    return inner

closure = close()  # closure is actually a pointer to inner()
closure()  # prints 5

Decorator

def logme(func):
    import logging  # because we don't want to require users to import it
    logging.basicConfig(level=logging.DEBUG)

    def inner():
        logging.debug("Called {}".format(func.__name__)
        return func()
    return inner

@logme
def say_hello():
        print("Hello there!")

say_hello()  # logs the call and then prints "Hello there!"

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment