[newbie] Looking for a good introduction to object oriented programming with Python
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Aug 6 02:08:38 EDT 2012
On Sun, 05 Aug 2012 21:14:04 -0400, Dennis Lee Bieber wrote:
> While I've probably used singletons (usually as sentinels in queues,
I don't know your code, but if I were to take a wild guess, I would say
that apart from None, and True/False, you probably haven't.
NotImplemented and Ellipsis are two other singletons in Python. It is
possible to program a Singleton class in pure Python, but most people
don't bother, because 1) singleton is probably the most over-used Design
Pattern of them all and 2) it's quite trivial to bypass the singleton-ness
of classes written in Python and create a second instance.
Or they use a module, which is not really a singleton but behaves like
one. Or if they want to show off their Pythonicity, they use the Borg
pattern.
The common sentinel idiom in Python is to do this:
SENTINEL = object()
# and later...
if value is SENTINEL:
pass
but of course that's not a singleton because it's only one instance out
of a vast number, and anyone can create another instance. Also it misses
the point of the Singleton pattern, that the (lone) instance should hold
state. The sentinel does not usually hold state.
> I suspect), but can't say that I've ever used a "factory function"...
If you've ever used an ordinary function decorator, you almost certainly
have.
If you've every created a closure, you definitely have.
--
Steven
More information about the Python-list
mailing list