Skip to content

Instantly share code, notes, and snippets.

@jasongrout
Created May 4, 2011 03:18
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 jasongrout/954701 to your computer and use it in GitHub Desktop.
Save jasongrout/954701 to your computer and use it in GitHub Desktop.
class mydecorator(object):
def __init__(self, times, repeats):
self.times=times
self.repeats=repeats
self.a=1
def __call__(self, func):
# here, we just need to return a new function. That new function will be what is actually stored in the name and run when I.
def myfunc(*args, **kwargs):
# *this* inner function is what is actually called
# here I also print the result of calling the function we
# are decorating
print "calling decorated function: %d times"%self.a
print func(*args, **kwargs)
print "finished"
self.a+=1
return myfunc
@mydecorator(times=2,repeats=4)
def f(x):
return x**2
f(3)
f(5)
f(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment