Skip to content

Instantly share code, notes, and snippets.

@ansig
Created March 3, 2018 12:05
Show Gist options
  • Save ansig/99fe8c5960025584343382e86c13c506 to your computer and use it in GitHub Desktop.
Save ansig/99fe8c5960025584343382e86c13c506 to your computer and use it in GitHub Desktop.
Python decorators
from functools import wraps
def wrap(element):
def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
return "<{0}>{1}</{0}>".format(element, func(*args, **kwargs))
return inner
return decorator
@wrap("html")
@wrap("body")
@wrap("p")
def say_hello(name):
return "Hello {0}!".format(name)
print say_hello("Anders")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment