__new__

E

Ethan Furman

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~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top