Bug? If not, how to work around it?
Michele Simionato
mis6 at pitt.edu
Fri Aug 8 07:42:15 EDT 2003
Gonçalo Rodrigues <op73418 at mail.telepac.pt> wrote in message news:<qp25jv04oct9v44vudgi5lt4t1ukqtnjt9 at 4ax.com>...
> It finally dawned on me why this is so: it's the way new classes work.
> In a sloppy language: a __getattr__ hook is like an instance
> attribute, but the iter machinery goes after the class not the
> instance so it misses __getattr__.
No, iter does not work even if you define __getattr__ at the metaclass
level, i.e. iter(c) is not type(c).__iter__(c) if "__iter__" is defined
via __getattr__ :
>>> class M(type):
def __getattr__(cls,name):
if name=='__iter__': return lambda self:iter([])
>>> class C: __metaclass__=M
>>> c=C()
>>> iter(c)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in -toplevel-
iter(c)
TypeError: iteration over non-sequence
>>> C.__iter__(c)
<listiterator object at 0x00A3DB30>
having-brought-more-confusion-in-your-head-yours-sincerely,
Michele
More information about the Python-list
mailing list