Issue with new-style classes and operators

Greg Ewing see_reply_address at something.invalid
Wed Nov 27 22:52:28 EST 2002


Jan Decaluwe wrote:

> But what makes operator methods so special that they should be treated
> so differently from ordinary methods?


I think it all stems from the unfortunate fact that
a class's dict contains a mixture of instance variables
of the class itself and metadata about instances of
that class.

This wasn't a problem with old-style classes, because
classes and instances were completely different types
of object, so you always knew what you were dealing
with.

But a new-style class object is both a class *and*
an instance at the same time. Applying the old-style
lookup rules to such an object would lead to ambiguity
in the case of things like __add__ and __getattr__ that
can apply to *any* type of object. If you looked up
one of those in a class, you wouldn't know whether
you had found an unbound method of instances of the class,
or a bound method of the class itself.

So something had to give, and what gave was the lookup
rules. By always starting in the class rather than the
instance, the ambiguity is removed.

I probably haven't explained this very well -- it all
tends to get rather brain-exploding, and I may have
got some details wrong. See PEPs 253 and 254 for the
full story (but mind your brain).

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list