Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Calling constructor but not initializer

Reply
Thread Tools

Calling constructor but not initializer

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-21-2007
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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      09-21-2007
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

 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      09-21-2007
Steven D'Aprano <(E-Mail Removed)> 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?
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-21-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling another constructor from a constructor Asfand Yar Qazi C++ 6 05-17-2004 03:16 PM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments