Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created July 7, 2017 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rewinfrey/9a7814d9bfcb977d1c58a61190123e3a to your computer and use it in GitHub Desktop.
Save rewinfrey/9a7814d9bfcb977d1c58a61190123e3a to your computer and use it in GitHub Desktop.
What is a Python Decorator?

Decorators in Python

Given a function:

def example():
  return

Given a function that takes as one of its inputs a function (we'll call this our decorator):

def example_decorator(function):
  # do something
  function()
  # do something else

We can then decorate the example function with example_decorator like so:

@example_decorator
def example():
  return

When we call example, the semantics are:

example() == example_decorator(example)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment