__new__

Discussion in 'Python' started by Ethan Furman, Aug 3, 2008.

  1. Ethan Furman

    Ethan Furman Guest

    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~
    Ethan Furman, Aug 3, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Simionato

    Overriding list.__new__

    Michele Simionato, Jul 3, 2003, in forum: Python
    Replies:
    1
    Views:
    318
    Terry Reedy
    Jul 3, 2003
  2. chenyu
    Replies:
    1
    Views:
    733
    Alex Martelli
    Nov 7, 2003
  3. Jp Calderone
    Replies:
    2
    Views:
    310
    Alex Martelli
    Nov 10, 2003
  4. Uwe Mayer

    __new__ to create copy

    Uwe Mayer, Feb 6, 2004, in forum: Python
    Replies:
    2
    Views:
    324
    Josiah Carlson
    Feb 6, 2004
  5. Steven Bethard
    Replies:
    2
    Views:
    446
    Steven Bethard
    Feb 16, 2005
Loading...

Share This Page