Created
May 4, 2011 03:18
-
-
Save jasongrout/954701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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