Python and the Singleton Pattern
Moshe Zadka
moshez at math.huji.ac.il
Wed Apr 7 03:53:15 EDT 1999
On Tue, 6 Apr 1999, Neelakantan Krishnaswami wrote:
> Isn't it possible to make use of Python's dynamism to turn any
> class into a singleton class, as needed?
<snipped implementation of class Singletonizer, which is basically a
class into a factory-function-which-always-returns the same object>
The problem with that, of course, is that it makes inheritance from
a singleton class impossible.
How about:
class MySingleton:
class _my_dummy: pass
_dummy=_my_dummy()
_initialized=0
def __init__(self, ....):
if initialized: return
......
def __getattr__(self, attr):
return _dummy.__dict__[attr]
def __setattr__(self, attr, val):
_dummy.__dict__[attr]=val
(I haven't tested it, but is seems like it should work)
--
Moshe Zadka <mzadka at geocities.com>.
QOTD: What fun to me! I'm not signing permanent.
More information about the Python-list
mailing list