__new__

Ethan Furman ethan at stoneleaf.us
Sun Aug 3 11:37:37 EDT 2008


Greetings, List!

I was browsing through the Decimal source today, and found this:

     # We're immutable, so use __new__ not __init__
     def __new__. . .
         self = object.__new__(cls)
	.
	.
	.
         return self

Out of curiousity I then tried this:
	--> import decimal
	--> d25 = decimal.Decimal(25)
	--> d25
	Decimal("25")
	--> d25._int
	(2, 5)
	--> d25._int = (1, 5)
	--> d25
	Decimal("15")
	--> d25 + 15
	Decimal("30")

If Decimals were really immutable, that should not have worked -- so in 
what sense are they immutable?  Does Python treat them as immutable if 
they are created with __new__ instead of __init__?

Any pointers to appropriate docs also greatly appreciated.

~Ethan~



More information about the Python-list mailing list