Bug? If not, how to work around it?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Aug 6 19:57:30 EDT 2003


On 6 Aug 2003 19:35:18 -0400, aahz at pythoncraft.com (Aahz) 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)
>>... 	
>>>>> 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?
>
>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.

Actually I came across this because of a little metaclass I was
coding. And my head hurts already as it is...

So, is it a bug? In other words, should I open a bug report?

With my best regards,
G. Rodrigues




More information about the Python-list mailing list