Bug? If not, how to work around it?
aahz at pythoncraft.com
Thu Aug 7 01:35:18 CEST 2003
In article <u8m2jvsus3om8r96ggnej9s7u3iecv3e29 at 4ax.com>,
Gonçalo Rodrigues <op73418 at mail.telepac.pt> wrote:
>>>> class Test(object):
>... def __init__(self, obj):
>... self.__obj = obj
>... def __getattr__(self, name):
>... return getattr(self.__obj, name)
>>>> a = Test()
><method-wrapper object at 0x0112CF30>
>Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
>TypeError: iteration over non-sequence
>Is this a bug? If not, how to code Test such that iter sees the
>__iter__ of the underlying object?
As Mark guessed, iter() goes directly to the attribute rather than using
the __getattr__ machinery of the class. However, you can intercept it
using a metaclass, but that requires a bit of fancy footwork to reach
down into the instance to get self.__obj.
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
More information about the Python-list