Skip to content

Instantly share code, notes, and snippets.

@squirly
Last active December 12, 2015 04:48
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 squirly/4716577 to your computer and use it in GitHub Desktop.
Save squirly/4716577 to your computer and use it in GitHub Desktop.
A Python decorators example
from functools import wraps
def cache(func):
func.cache = None
@wraps(func)
def inner(*a, **k):
if func.cache is None:
func.cache = func(*a, **k)
return func.cache
return inner
def debug(func):
@wraps(wraps)
def inner(*a, **k):
print(str(a) + str(k))
return func(*a, **k)
return inner
if '__main__'==__name__:
@cache
@debug
def add(a, b):
return a + b
print(add(1, 2))
print(add(3, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment