Created
June 23, 2012 20:59
-
-
Save jasonLaster/2979952 to your computer and use it in GitHub Desktop.
decorating
This file contains hidden or 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 decorate(): | |
def __init__(self, func, *a): | |
self.func = func | |
def __call__(self, *a): | |
return self.func(*a) | |
@decorate | |
def func(arg): | |
print arg | |
func('foo') |
This file contains hidden or 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 curried(object): | |
def __init__(self, func, *a): | |
self.func = func | |
self.args = a | |
def __call__(self, *a): | |
args = self.args + a | |
if len(args) < self.func.func_code.co_argcount: | |
return curried(self.func, *args) | |
else: | |
return self.func(*args) | |
@curried | |
def add(a,b): | |
return a + b | |
# print board | |
row_size = 8 | |
for i in range(1,row_size+1): | |
addi = add((i-1)*row_size+1) | |
print [addi(j) for j in range(0,row_size)] |
This file contains hidden or 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 decorate(): | |
def __init__(self, func, *a): | |
self.func = func | |
self.is_open = False | |
def __call__(self, *a): | |
if self.is_open: | |
return self.func(*a) | |
def open(self): | |
self.is_open = True | |
@decorate | |
def gift(item): | |
print item | |
gift('toy car') | |
gift.open() | |
gift('toy car') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment