suggestions for functional style (singleton pattern?)

Michael Torrie torriem at gmail.com
Sun Mar 1 00:05:16 EST 2015


On 02/28/2015 09:29 PM, yves at zioup.com wrote:
>> Problem 4:
>> You speak of a singleton. But you haven't implemented one. It is not
>> clear from your code if this class should be a singleton. I'm guessing
>> not. Singletons are in fact rare. Well, let me put it another way:
>> Good reasons to code a singleton are rare.
> 
> Thanks. I hadn't realise "singleton" meant a class built such that it could
> not be instanciated more than once, I thought it corresponded to a pattern
> where only one object is ever created from a given class.

Is not "only one object is ever created from a given class" the same as
"could not be [instantiated] more than once?"

To be clear, singletons are commonly used in Python every time you
import a module.  A module *is* a singleton pattern, particularly one
that maintains state.  I use sometimes use this feature for sharing
config and other data between other modules (global state when it's
required).

"import module" essentially does the one-time instantiation if it's a
new import; top-level code in the module is run once on first import,
where it acts like a constructor.  So it functions as a singleton.  As
near as I can tell there's very little reason to make a class-based
singleton in Python, since a module has the same effect and is cleaner.



More information about the Python-list mailing list