[Tutor] Re-instantiate within __init__

Jan Eden lists at janeden.org
Wed Apr 19 16:39:47 CEST 2006


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>

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.

Thanks,

Jan
-- 
Any sufficiently advanced technology is insufficiently documented.


More information about the Tutor mailing list