Created
November 30, 2012 10:23
-
-
Save mayask/4175000 to your computer and use it in GitHub Desktop.
Old-style class decorator example
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
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