Bug? If not, how to work around it?
op73418 at mail.telepac.pt
Thu Aug 7 19:38:07 CEST 2003
On Thu, 7 Aug 2003 12:39:06 -0400, "Terry Reedy" <tjreedy at udel.edu>
>"Gonçalo Rodrigues" <op73418 at mail.telepac.pt> wrote in message
>news:md94jvccu02b9dv5890k34629rkot79roj at 4ax.com...
>> On Thu, 07 Aug 2003 00:54:03 +0100, Gonçalo Rodrigues
>> <op73418 at mail.telepac.pt> wrote:
>> >On Wed, 6 Aug 2003 19:46:39 -0400, "Terry Reedy" <tjreedy at udel.edu>
>> >>If you don't want to do what works,
>> >>why ask us to bother answering?
>> >Because it may not work. That is, the use case I have in mind
>> >as proxy for an object that may or may not have __iter__. IOW if I
>> >stick __iter__ I have to code a second class, etc... etc... Oh
>> Just to make things clearer, having iter(self.__obj) as in
>> def __iter__(self):
>> return iter(self.__obj)
>> Would mean that my proxy class would become an iterable. And that is
>> not what I want because *other* parts in my code check that an
>> is in iterable by looking for __iter__.
>As I understand your clarification, you want the proxy to actually be
>iterable (via delegation) when given as an arg to iter(), but to not
>look iterable to other code (call it test_iterable()). And your
>test_iterable() *currently* looks only for the presence/absence of the
>very thing needed to make iter() work.
To look an iterable by delegation *if* the underlying object is, yes.
>It is definitely not a Python bug that this does not work.
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__.
So my test_iterable (in your nomenclature) is wrongly coded because it
gives false positives.
Thanks for the input. But since something has to change anyway, the
best idea seems to be to stick __iter__ in the proxy class after all
and change the other code that was counting on testing iterableness by
looking __iter__. This will involve some changes, but it's the "least
worse of all evils."
With my best regards,
More information about the Python-list