Spurious coercing? [was: Re: 'if foo' calls getattr?!?]

François Pinard pinard at iro.umontreal.ca
Tue Jun 6 12:32:30 EDT 2000


Moshe Zadka <moshez at server.python.net> writes:

> On Tue, 31 Aug 1999, Moshe Zadka wrote:

> > Bzzt! foo!=None is slower then foo is not None, but not by much: it will
> > first check whether type(foo) is not type(None), so it is pretty fast.

> > truth-and-nothing-are-always-popular-subjects-ly y'rs, Z.

> Sorry to reply to my own posts, but there was a mistake: foor!=None will
> make a call to __getattr__ for __cmp__, so it will be very slow.

And I would presume that if __cmp__ happened to be found, a call to
__coerce__ would be tried before going any further?

I was surprised to see, a little while ago, that __coerce__ is called even
if the two arguments have the same proper type.  I would have imagined that
the __coerce__ is spared, when both arguments are already of the given
class type.  Would someone have an example to illustrate that calling
__coerce__ may be meaningful when types do not need be coerced?

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list