[Tutor] singleton pattern
Michael P. Reilly
arcege@speakeasy.net
Thu, 17 May 2001 14:47:47 -0400 (EDT)
alan.gauld@bt.com wrote
> Alan G.
> > Guido once also suggested that replacing __class__ (which is
> > something I believe Alan was attempting to do)
>
> I wasn't replacing class just returning a pointer to the
> original instance as an attribute of the Exception.
This was in an earlier email you had sent out (something about
"self.class = ..." within the __init__ method).
It was something more like:
class FactInst:
pass
class Factory:
def __init__(self, klass):
self.klass = klass
def __call__(self, *args, **kws)
# create a dummy instance
inst = FactInst()
# change its class
inst.__class__ = self.klass
# call the initialization routine
apply(inst.__init__, args, kws)
# return the re-class-fied instance
return inst
class Spam:
def __init__(self, msg):
self.msg = msg
def __str__(self):
return msg
FactSpam = Factory(Spam) # a new factory instance to be used like a class
brkfast = FactSpam("spam, spam, eggs & spam") # a new "Spam" instance
print brkfast
It's been a few years since I read that thread, and I probably don't have
things exactly right, but the idea is to replace some of the "important"
bindings within an instance with other class info altogether.
-Arcege
--
+----------------------------------+-----------------------------------+
| Michael P. Reilly | arcege@speakeasy.net |