Bug? If not, how to work around it?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Aug 6 18:22:14 EDT 2003


On Wed, 06 Aug 2003 14:58:02 -0700, Mark Day <mday at apple.com> wrote:

>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)
>> ...    
>> 
>> Now:
>> 
>> >>> a = Test([])
>> >>> a.__iter__
>> <method-wrapper object at 0x0112CF30>
>> >>> iter(a)
>> 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?
>
>I'm guessing that iter() is looking for an __iter__ attribute without
>going through __getattr__ to find it.  So, I tried adding the following
>method to the Test class:
>
>    def __iter__(self):
>        return self.__obj.__iter__
>
>but that returned the following error:
>
>TypeError: iter() returned non-iterator of type 'method-wrapper'
>

You forgot the parenthesis.

>I changed the __iter__ method to the following, and it seems to do what
>you want:
>
>    def __iter__(self):
>        return iter(self.__obj)
>

But this is precisely what I don't want to do.

With my best regards,




More information about the Python-list mailing list