From: moshez at math.huji.ac.il (Moshe Zadka)
Date: Wed, 7 Apr 1999 09:53:15 +0200
Subject: Python and the Singleton Pattern
In-Reply-To: <7ee4si$sec$2@antiochus.ultra.net>
References: <slrn7g7d89.33j.gregory.p.green@skorch.rt.cs.boeing.com> <37048D53.5D1ED05F@lemburg.com> <y0jk8vuamz6.fsf@vier.idi.ntnu.no> <7ee4si$sec$2@antiochus.ultra.net>
Message-ID: <Pine.SUN.3.95-heb-2.07.990407094721.8700E-100000@sunset.ma.huji.ac.il>
X-UID: 825                                                  

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.





