ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Re-instantiate within __init__
by Alan Gauld other posts by this author
Apr 19 2006 10:24AM messages near this date
Re: [Tutor] Re-instantiate within __init__ | [Tutor] OOPs Concept
>  is it correct that an object cannot be re-instantiated within it's 
>  __init__ method?

There are some tricks you can pull but the object is actually instantiated
before the init gets called. Really init is for initialisation of the 
instance,
it's not a true constructor.

>  Background: I need to create a new object upon instantiation
>  when a database query returns no records.

I'd probably create a factory function to do this that
returns the appropriate type of instance.

def makeDbResponseInstance(queryStatus):
      if queryStatus:
         return DBClass()
      else: return Emptyclass()

If the two classes are subclassed from a common ancestor you
might put the factory into the class as a class method, but I'd
probably just keep it as a function. That keeps the two class's
definitions clean and decoupled from the instantiation decision
which isn't really their responsibility - children don't choose to be
born!.

Alan G. 

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Jan Eden
Danny Yoo
Kent Johnson
Alan Gauld

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved