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

François Pinard pinard at iro.umontreal.ca
Wed Jun 7 09:14:39 EDT 2000


scarblac-spamtrap at pino.selwerd.nl (Remco Gerlich) écrit:
> François Pinard wrote in comp.lang.python:

> > I saw many emails in which people were surprised by Python behaviour,
> > and I interpret the surprise as a sign that predictability suffers.
> > For example, A.__cmp__(B) will not be called for an equality comparison
> > when A "is" B.  Yet, given the meaning of __cmp__, this sounds absolutely
> > correct to me.

> > Also given the meaning of __coerce__, I would consider just equally
> > correct to avoid calling A.__coerce__(B) when type(A) "is" type(B).
> > The spontaneous predictable thing, here, is that __coerce__ should *not*
> > be used when it is evident it should not.

> But when both types are InstanceType, __coerce__ may still be needed,
> right?

I see your point.  `type()' has a rather generic value for instances,
and so, my way of putting it is not exact.  I really meant that if A and B
are instances from the very same class (which is expected to be a frequent
case), coercing should not be needed, and the __coerce__ call should then
be avoided, on the same principle that __cmp__ is avoided whenever possible.

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






More information about the Python-list mailing list