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

Max Moroz maxmoroz at gmail.com
Wed Apr 18 11:57:37 CEST 2012

Simon Sapin <simon.sapin at kozea.fr> wrote:
> I agree that the definition of "immutable" is not very clear, but I don’t
> think that your Card class is immutable.

I shouldn't have used the word "immutable"; it is a bit confusing and
distracts from my real concern.

I'm really just trying to get a guarantee from the language that would
make my original code safe. As is, it relies on a very reasonable, but
undocumented, assumption about the behavior of built-in classes'
__new__ method

The exact guarantee I need is: "Any built-in class' __new__ method
called with the cls argument set to a user-defined subclass, will
always return a new instance of type cls."

(Emphasis on "new instance" - as opposed to a reference to an existing object.)

Nick Coghlan <ncoghlan at gmail.com> wrote:
> and you'd be better of just writing Card properly -
> inheriting from int for an object that should model a "value, suit"
> 2-tuple is a bad idea. Using collections.namedtuple would be a much
> better option.

My example might have been poor. Still, I have use cases for objects
nearly identical to `int`, `tuple`, etc., but where I want to
distinguish two objects created at different times (place them both in
a set, compare them as unequal, etc.).

Thanks for your comments.


More information about the Python-ideas mailing list