Singleton vs Proxies DP (was Re: Solution: Direct access to Printer I/O lines)

Moshe Zadka moshez at zadka.site.co.il
Sun Dec 17 06:58:03 EST 2000


On Sun, 17 Dec 2000 03:26:32 GMT, "Rainer Deyke" <root at rainerdeyke.com> wrote:

> I consider the singleton pattern as stated in the GOF to be overly
> cumbersome in the context of Python.  (It is necessary in C++ because of
> object creation order.)

Not only that -- laziness is also important.
What I mean is that I *do* like the fact that it initializes when *I*
tell it to. (I don't like module initialization to do more then define
functions and classes...)

The idiom I use is 

class _Foo:

	...write a regular class here...

foo = None
def Foo():
	global foo
	if foo is not None:
		return foo
	foo = _Foo()
	return foo

This has the added *feature* that it is not possible to inherit from Foo.
I'm against inheritance as a rule, and inheritance from singletons positively
shocks me, given that it is so easy in Python to use delegation. Delegation
to singletons is good, and what's more, the semantics are clear-cut.
-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!




More information about the Python-list mailing list