Calling constructor but not initializer

S

Steven D'Aprano

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?
 
S

Steve Holden

Steven said:
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
 
H

Hrvoje Niksic

Steven D'Aprano said:
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?
 
S

Steven D'Aprano

On Fri, 21 Sep 2007 10:47:02 -0400, Steve Holden wrote:

[snippity-doo-dah]
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top