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 Kent Johnson other posts by this author
Apr 19 2006 8:33AM messages near this date
Re: [Tutor] Re-instantiate within __init__ | Re: [Tutor] Re-instantiate within __init__
Jan Eden wrote:
>  Hi,
>  
>  is it correct that an object cannot be re-instantiated within it's __init__ method?
>  
>  I tried:
>  
>  class Omega:
>      def Display(self):
>          print self
>          
>  class Alpha(Omega):
>      def __init__(self):
>          self = Beta()
>          
>  class Beta(Omega):
>      def __init__(self):
>          pass
>                  
>  objectus = Alpha()
>  objectus.Display()
>  
>  which prints
>  
>  <__main__.Alpha instance at 0x54d50>

Binding to self in a method just changes the local value of self, it 
doesn't change the object itself. The only thing special about 'self' is 
the way it is bound before the method is entered; inside the method it 
is a local name like any other.

To bind objectus to a new object you have to assign to objectus. But I 
suspect you are really looking for a way to change the behaviour of the 
object bound to objectus.
>  
>  Background: I need to create a new object upon instantiation when a
database query returns no records. The rest of the program should just
use the new object.
>  
>  Is there any way to achieve this? I can always place the equivalent
>  of
'self = Beta()' in the Display() function:
>  
>      def Display(self):
>          if self.not_found:
>              self = Beta()
>              self.Display()
>              return
>          print self
>  
>   but this does not look very elegant.

I don't understand what you are trying to do, but here are some ideas:
- have Display() look at self.not_found itself, or pass self.not_found 
as a parameter to display.
- use a factory method to create a class of the correct type
- change self.Display to be the correct function
- you can change the class of an object at runtime by assigning to its 
__class__ attribute. This may be what you are looking for but I suspect 
there is a simpler, less magic solution
- I'm not sure why classes are needed at all here.

Can you give a more complete example of what you are trying to do? Even 
if your code above worked the way you want it to, it wouldn't do 
anything interesting, since Alpha and Beta have exactly the same 
attributes and behaviour.

Kent

_______________________________________________
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
© 2004 ActiveState, a division of Sophos All rights reserved