[Python-ideas] Providing a guarantee that instances of user-defined classes have distinct identities

Simon Sapin simon.sapin at kozea.fr
Wed Apr 18 07:26:06 CEST 2012


Le 18/04/2012 03:23, Max Moroz a écrit :
> Nowhere in the documentation is it clearly defined which objects are
> considered "immutable" for the purpose of this promise. As a result, a
> Python implementation, now or in the future, may decide that it's ok to
> return a reference to an existing object when a Card instance is created
> - since arguably, class Card is immutable (since it derives from an
> immutable base class, and doesn't add any new attributes).

Hi,

I agree that the definition of "immutable" is not very clear, but I 
don’t think that your Card class is immutable.

As Card inherits without __slots__, it gets a __dict__ and can hold 
arbitrary attributes. Even if none of its methods do so, this is 
perfectly okay:

x = Card(14)
y = Card(14)
x.foo = 42
y.foo  # AttributeError

Because of their __dict__, Card objects can never be considered immutable.
(Now, I’m not sure what would happen with an empty __slots__.)

Regards,
-- 
Simon Sapin



More information about the Python-ideas mailing list