RE: RE: Python 2.3 compatibility?

"Bauer, Georg" <[email protected]> Tue, 20 Jan 2004 15:09:35 +0100
Newsgroups gmane.comp.pythin.pyds.devel
Message-ID <[email protected]>
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.

bye, Georg