Can __iter__ be used as a classmethod?
Lulu of the Lotus-Eaters
mertz at gnosis.cx
Mon Mar 3 18:17:43 EST 2003
More information about the Python-list mailing list
Mon Mar 3 18:17:43 EST 2003
- Previous message (by thread): Can __iter__ be used as a classmethod?
- Next message (by thread): Can __iter__ be used as a classmethod?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Bjorn Pettersen" <BPettersen at NAREX.com> wrote previously: |I would like to be able to traverse the content of a class (not an |instance) using a regular for loop: "for item in MyClass:...". If you want to add methods to a *class*, you create the class using a custom metaclass: % cat classiter.py from __future__ import generators class ClassIter(object): vals = ['a', 'b', 'c'] class __metaclass__(type): def __iter__(cls): print 'iter' return cls.items() def items(cls): print 'items' for item in cls.vals: yield item items = classmethod(items) for x in ClassIter: print x % python classiter.py iter items a b c Yours, Lulu... -- mertz@ | The specter of free information is haunting the `Net! All the gnosis | powers of IP- and crypto-tyranny have entered into an unholy .cx | alliance...ideas have nothing to lose but their chains. Unite | against "intellectual property" and anti-privacy regimes! -------------------------------------------------------------------------
- Previous message (by thread): Can __iter__ be used as a classmethod?
- Next message (by thread): Can __iter__ be used as a classmethod?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list