Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created June 23, 2012 20:59
Show Gist options
  • Save jasonLaster/2979952 to your computer and use it in GitHub Desktop.
Save jasonLaster/2979952 to your computer and use it in GitHub Desktop.
decorating
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')
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)]
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