Bug? If not, how to work around it?

Terry Reedy tjreedy at udel.edu
Thu Aug 7 12:39:06 EDT 2003


"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
serves
> >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
well...

> 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
object
> 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.

It is definitely not a Python bug that this does not work.

But I am still not sure of your spec for test_iterable().  If you want
it to always return false for the proxies, it needs another indicator
that iter() doesn't use (which is what you were trying to do with your
__getattr__).  So add the following line to your proxy class:

    not_iterable = True # or 1 if pre2.2

and have test_iterable check this next if it does find __iter__.

def test_iterable(obj): #untested
    return hasattr(obj, '__iter__') and not hasattr(obj,
'not_iterable')

If you want it to return true or false (for proxies) according to the
wrapped object, then it must directly or indirectly test the wrapped
object.

Terry J. Reedy






More information about the Python-list mailing list