__new__ does not call __init__ as described in descrintro.html (WAS:Can __new__ prevent __init__ fro

Discussion in 'Python' started by Steven Bethard, Feb 16, 2005.

  1. Felix Wiemann wrote:
    > Steven Bethard wrote:
    >> http://www.python.org/2.2.3/descrintro.html#__new__

    >

    [snip]
    >
    > I'm just seeing that the web page says:
    >
    > | If you return an existing object, the constructor call will still
    > | call its __init__ method. If you return an object of a different
    > | class, its __init__ method will be called.
    >
    > However, the latter doesn't seem to be true, or am I missing
    > something?
    >
    >>>>class A(object):

    > ... def __init__(self):
    > ... print 'Init of A.'
    > ...
    >
    >>>>instance = A()

    > Init of A.
    >
    >>>>class B(object):

    > ... def __new__(self):
    > ... return instance
    > ... def __init__(self):
    > ... print 'Init of B.'
    > ...
    >
    >>>>B() # <--------- A's __init__ is *not* called.

    > <__main__.A object at 0x4062424c>
    >
    >>>>instance = object.__new__(B)
    >>>>B() # <--------- B's __init__ is called

    > Init of B.
    > <__main__.B object at 0x406243ec>
    >
    > So there seems to be some type-checking in type.__call__.


    Yeah, I saw the same thing in playing around with this. Don't know what
    to make of it. I wonder if we should file a documentation bug? I can't
    find __new__ explained anywhere in the Language Reference. Can
    documentation bugs be filed for descrintro.html?

    STeVe
    Steven Bethard, Feb 16, 2005
    #1
    1. Advertising

  2. Steven Bethard

    Aahz Guest

    In article <>,
    Steven Bethard <> wrote:
    >
    >Yeah, I saw the same thing in playing around with this. Don't know
    >what to make of it. I wonder if we should file a documentation bug? I
    >can't find __new__ explained anywhere in the Language Reference. Can
    >documentation bugs be filed for descrintro.html?


    Absolutely! And they're especially welcome if they contain suggested
    replacement text.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "The joy of coding Python should be in seeing short, concise, readable
    classes that express a lot of action in a small amount of clear code --
    not in reams of trivial code that bores the reader to death." --GvR
    Aahz, Feb 16, 2005
    #2
    1. Advertising

  3. Re: __new__ does not call __init__ as described in descrintro.html

    Aahz wrote:
    > In article <>,
    > Steven Bethard <> wrote:
    >
    >>Yeah, I saw the same thing in playing around with this. Don't know
    >>what to make of it. I wonder if we should file a documentation bug? I
    >>can't find __new__ explained anywhere in the Language Reference. Can
    >>documentation bugs be filed for descrintro.html?

    >
    > Absolutely! And they're especially welcome if they contain suggested
    > replacement text.


    Cool. I added a documentation bug:

    http://sourceforge.net/tracker/?func=detail&aid=1123716&group_id=5470&atid=105470

    Steve
    Steven Bethard, Feb 16, 2005
    #3
    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. chenyu
    Replies:
    1
    Views:
    725
    Alex Martelli
    Nov 7, 2003
  2. Felix Wiemann
    Replies:
    9
    Views:
    379
    Martin
    Feb 16, 2005
  3. Edward C. Jones

    __new__, __init__ and pickle

    Edward C. Jones, Jun 30, 2005, in forum: Python
    Replies:
    0
    Views:
    363
    Edward C. Jones
    Jun 30, 2005
  4. Daniel Klein

    __init__ vs __new__

    Daniel Klein, Jan 11, 2007, in forum: Python
    Replies:
    1
    Views:
    340
    Neil Cerutti
    Jan 11, 2007
  5. luca72
    Replies:
    0
    Views:
    269
    luca72
    Sep 3, 2010
Loading...

Share This Page