Calling constructor but not initializer

Discussion in 'Python' started by Steven D'Aprano, Sep 21, 2007.

  1. I have a class that has a distinct "empty" state. In the empty state, it
    shouldn't have any data attributes, but it should still have methods.

    The analogy is with a list: an empty list still has methods like append()
    etc. but it has no "data", if by data you mean items in the list.

    I can construct an empty instance in the __new__ constructor, and I can
    initialize an non-empty instance in the __init__ initializer, but I can't
    think of any good way to stop __init__ from being called if the instance
    is empty. In pseudo-code, I want to do something like this:

    class Parrot(object):
    def __new__(cls, data):
    construct a new empty instance
    if data is None:
    return that empty instance
    else:
    call __init__ on the instance to populate it
    return the non-empty instance


    but of course __init__ is automatically called.


    Any suggestions for doing something like this?


    --
    Steven
     
    Steven D'Aprano, Sep 21, 2007
    #1
    1. Advertising

  2. Steven D'Aprano

    Steve Holden Guest

    Steven D'Aprano wrote:
    > I have a class that has a distinct "empty" state. In the empty state, it
    > shouldn't have any data attributes, but it should still have methods.
    >
    > The analogy is with a list: an empty list still has methods like append()
    > etc. but it has no "data", if by data you mean items in the list.
    >
    > I can construct an empty instance in the __new__ constructor, and I can
    > initialize an non-empty instance in the __init__ initializer, but I can't
    > think of any good way to stop __init__ from being called if the instance
    > is empty. In pseudo-code, I want to do something like this:
    >
    > class Parrot(object):
    > def __new__(cls, data):
    > construct a new empty instance
    > if data is None:
    > return that empty instance
    > else:
    > call __init__ on the instance to populate it
    > return the non-empty instance
    >
    >
    > but of course __init__ is automatically called.
    >
    >
    > Any suggestions for doing something like this?
    >
    >

    Easy: use a method whose name is something other than __init__, then
    don't bother to implement __init__. Note that __new__ shouldn't call
    __init__ anyway, that's done by the instance creation mechanism.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
     
    Steve Holden, Sep 21, 2007
    #2
    1. Advertising

  3. Steven D'Aprano <> writes:

    > I can construct an empty instance in the __new__ constructor, and I
    > can initialize an non-empty instance in the __init__ initializer,
    > but I can't think of any good way to stop __init__ from being called
    > if the instance is empty. In pseudo-code, I want to do something
    > like this:
    >
    > class Parrot(object):
    > def __new__(cls, data):
    > construct a new empty instance
    > if data is None:
    > return that empty instance
    > else:
    > call __init__ on the instance to populate it
    > return the non-empty instance


    Suggestion 1: since you "construct a new empty instance" in both
    cases, simply move the entire logic to __init__.

    Suggestion 2: name your initialization method something other than
    __init__ and the
    calling-type-object-automatically-calls-__init__-after-__new__ simply
    disappears.

    Can you specify the way you'd like to instantiate the class?
     
    Hrvoje Niksic, Sep 21, 2007
    #3
  4. On Fri, 21 Sep 2007 10:47:02 -0400, Steve Holden wrote:

    [snippity-doo-dah]

    >> but of course __init__ is automatically called.
    >>
    >>
    >> Any suggestions for doing something like this?
    >>
    >>

    > Easy: use a method whose name is something other than __init__, then
    > don't bother to implement __init__. Note that __new__ shouldn't call
    > __init__ anyway, that's done by the instance creation mechanism.


    *stares at post*

    You know, I must have been REALLY tired last night, because that is
    extraordinarily obvious.

    Even more obvious is not to use __new__ at all, and just wrap the body of
    the __init__ in an "if instance should not be empty" clause.

    Thanks to everyone for answering my stupid question without laughing.


    --
    Steven.
     
    Steven D'Aprano, Sep 22, 2007
    #4
    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. Razvan
    Replies:
    7
    Views:
    17,701
    Lee Fesperman
    Jul 4, 2004
  2. Ed Thompson

    static initializer vs constructor

    Ed Thompson, Oct 9, 2004, in forum: Java
    Replies:
    9
    Views:
    948
    Chris Uppal
    Oct 11, 2004
  3. Matthias Kaeppler

    Initializer vs. Constructor assignment

    Matthias Kaeppler, May 7, 2005, in forum: Java
    Replies:
    4
    Views:
    1,062
    Tor Iver Wilhelmsen
    May 8, 2005
  4. Giulio
    Replies:
    9
    Views:
    1,067
    Patrick Kowalzick
    Jun 25, 2003
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,344
Loading...

Share This Page