[Tutor] singleton pattern

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 17 May 2001 00:32:05 +0200


On  0, alan.gauld@bt.com wrote:
> Speculative code warning - I haven't tried this, consider it pseudo code...
> 
> Couldn't you do this:
> 
> class Factory:
>    instance = None # class variable
>    def __init__(self,class):
>       if Factory.instance is None:
>          Factory.instance = self
>       else: self = Factory.instance
>       self.class = class
> 
>    def makeInstance(self):
>       return self.class()
> 
> Now reassigning self is normally a very bad thing 
> to do but it might work for this case?

It doesn't actually do anything. You can rebind the local variable 'self' to
some other object, but that doesn't magically turn the new instance into
something else. Whatever was referring to the new instance before is still
referring to it. At the end of __init__, the self variable is forgotten
again. 

> I need a python prompt...

Besides, 'class' is a reserved word :)

-- 
Remco Gerlich