Skip to content

Instantly share code, notes, and snippets.

@mayask
Created November 30, 2012 10:23
Show Gist options
  • Save mayask/4175000 to your computer and use it in GitHub Desktop.
Save mayask/4175000 to your computer and use it in GitHub Desktop.
Old-style class decorator example
def classdec(cls):
def stat(itermeth):
def inner(self):
for i in itermeth(self):
print "stat:", i
yield i
return inner
cls.__iter__ = stat(cls.__iter__)
return cls
class A(object):
def __init__(self, x):
self.x = x
def __iter__(self):
return iter(range(self.x))
A = classdec(A)
a = A(6)
for i in a:
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment