osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

singleton ... again


On 02/13/2014 03:50 AM, Ned Batchelder wrote:
> On 2/13/14 4:00 AM, Piet van Oostrum wrote:
>> Ben Finney <ben+python at benfinney.id.au> writes:
>>
>>> Gregory Ewing <greg.ewing at canterbury.ac.nz> writes:
>>>
>>>> Roy Smith wrote:
>>>>> It looks to me like he's trying to implement a classic Gang of Four
>>>>> singleton pattern.
>>>>
>>>> Which I've never really seen the point of in Python, or any other
>>>> language for that matter. Just create one instance of the class during
>>>> initialisation, put it in a global somewhere, and use it thereafter.
>>>
>>> Make that ?somewhere? a module namespace, and you effectively have a
>>> Singleton for all practical purposes. So yes, I see the point of it; but
>>> we already have it built in :-)
>>
>> There is a use case for a singleton class: when creating the singleton
>> object takes considerable resources and you don't need it always in your
>> program.
>>
>
> I still don't see it.  To convince me that a singleton class makes sense, you'd have to explain why by virtue of the
> class's very nature, it never makes sense for there ever to be more than one of them.

Say you have a database with a column that can only have a handful of values (like an enumeration, for instance) and 
this database can have hundreds of thousands of rows.  When you're working with all those rows at once having just one 
object for the third enum value is a useful optimization.

Say you have a class that represents serial ports or your computer.  You should get the same object every time you ask 
for SerialPort(2).

--
~Ethan~