Re: RE: Python 2.3 compatibility?

Bob Ippolito <bob-Zl9L/[email protected]> Tue, 20 Jan 2004 09:22:44 -0500
Newsgroups gmane.comp.pythin.pyds.devel
Message-ID <[email protected]>
On Jan 20, 2004, at 9:09 AM, Bauer, Georg wrote:

> Hi!
>
>>>         KeyError: '__nonzero__'
>>
>> Yep, that's one of those weirdos. In lin 306 in UpstreamTool.py, the
>> variable tool is filled with an instance of StandardTool by a
>> getToolByNamespace call. The "if tool" line in Python 2.2
>> checks the fact
>> wether it's None or not-None. In 2.3 this was expanded and
>> boolean checking
>> with class instances (even classic classes) seems allways to
>> go through a
>> __nonzero__ method. That's not defined in class StandardTool
>> and so this
>> problem happens.
>
> I digged a bit deeper on this one and believe this is actually a bit 
> weird,
> maybe even a bug in Python 2.3. From the Python 2.3 reference on
> customizations of classes:
>
> --- cut here ---
>
> __nonzero__(self)
>
> Called to implement truth value testing, and the built-in operation 
> bool();
> should return False or True, or their integer equivalents 0 or 1. When 
> this
> method is not defined, __len__() is called, if it is defined (see 
> below). If
> a class defines neither __len__() nor __nonzero__(), all its instances 
> are
> considered true.
>
> --- excerpt ends ---
>
> So if I don't implement a __nonzero__ method, every instance should be
> considered true. That's what happens with Python 2.2. But with your 
> problem
> (and the other one I was reported), it looks like somehow Python 2.3
> disagrees and thinks a __nonzero__ method is obligatory. Maybe anyone 
> with
> more experience with Python 2.3 can jump in and give an explanation 
> what is
> happening here.

Are you implementing __getattr__?  Because that behaves a lot 
differently for classic and new-style classes.

 >>> class Foo:
...   def __getattr__(self, foo):
...     print foo
...
 >>> f = Foo()
 >>> f
__repr__
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable


 >>> class F(object):
...   def __getattr__(self, foo):
...     print foo
...
 >>>
 >>> f = F()
 >>> f
<__main__.F object at 0x554d0>


-bob
smime.p7s (application/pkcs7-signature, 2.3 KB) - not displayed