Issue with new-style classes and operators

Jan Decaluwe jan at jandecaluwe.com
Wed Nov 27 04:22:20 EST 2002


Greg Ewing wrote:
> 
> Jan Decaluwe wrote:
> 
> > However, if I use inheritance from an immutable
> > type, the subtype is automatically immutable also, right?
> 
> Not necessarily -- there's nothing stopping you giving
> your subclass some mutable state and providing
> methods for mutating it.

Ok, let me explain better. Suppose - for the sake of the
argument - that I have a legitimate need for mutable numbers.

The point is then that I want to "inherit" the operators
from the (mutable) value. 

This is trivial to do with old-style classes and delegation,
but this technique doesn't work with new-style classes, as my initial
example shows. 

If on the other hand, I use "real" inheritance from an 
immutable type, there's no way I see to achieve a subtype
with a mutable underlying value. On *this* point, the
documentation on new-style classes is actually very 
clear: in fact, __new__ was introduced to guarantee
that subclassing doesn't break immutability. 

So I'm stuck. OTOH, I'm still not convinced that the
new behavior of __getattr__ is a feature instead of
a bug.

Regards, Jan

--
Jan Decaluwe 
mailto:jan at jandecaluwe.com



More information about the Python-list mailing list