Re: Generic singleton

2010-03-05 Thread Terry Reedy
On 3/4/2010 10:32 PM, Steven D'Aprano wrote: Python does have it's own singletons, like None, True and False. True and False are not singletons. For some reason, they behave quite differently: Because they are quite different. NoneType fails if you try to instantiate it again, Because

Re: Generic singleton

2010-03-05 Thread Steven D'Aprano
On Fri, 05 Mar 2010 11:57:13 -0500, Terry Reedy wrote: On 3/4/2010 10:32 PM, Steven D'Aprano wrote: Python does have it's own singletons, like None, True and False. True and False are not singletons. Duotons? Doubletons? t1 = bool(1) t2 = bool(1) t1 is t2 True t1 is t2 is bool(this

Re: Generic singleton

2010-03-05 Thread Terry Reedy
On 3/5/2010 1:01 PM, Steven D'Aprano wrote: On Fri, 05 Mar 2010 11:57:13 -0500, Terry Reedy wrote: On 3/4/2010 10:32 PM, Steven D'Aprano wrote: Python does have it's own singletons, like None, True and False. True and False are not singletons. Duotons? Doubletons? The latter is what I

Re: Generic singleton

2010-03-05 Thread Steven D'Aprano
On Fri, 05 Mar 2010 16:25:46 -0500, Terry Reedy wrote: On 3/5/2010 1:01 PM, Steven D'Aprano wrote: On Fri, 05 Mar 2010 11:57:13 -0500, Terry Reedy wrote: On 3/4/2010 10:32 PM, Steven D'Aprano wrote: Python does have it's own singletons, like None, True and False. True and False are not

Re: Generic singleton

2010-03-05 Thread Gregory Ewing
I think the important difference between None and booleans wrt singleton behaviour is that things are often compared with None using is, so it's quite important that there only be one instance of NoneType around, and it makes sense not to give people the false impression that they can create

Re: Generic singleton

2010-03-05 Thread Gregory Ewing
Steven D'Aprano wrote: While Doubleton or even Tripleton sound cute, once you get to large counts it all starts getting ugly and horrible. Polyton? Blah. Tupleton? -- Greg -- http://mail.python.org/mailman/listinfo/python-list

Re: Generic singleton

2010-03-04 Thread mk
Steven D'Aprano wrote: Groan. What is it with the Singleton design pattern? It is one of the least useful design patterns, and yet it's *everywhere* in Java and C++ world. It's useful when larking about in language internals for learning purposes, for instance. I don't recall ever actually

Re: Generic singleton

2010-03-04 Thread Duncan Booth
Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Wed, 03 Mar 2010 19:54:52 +0100, mk wrote: Hello, So I set out to write generic singleton, i.e. the one that would do a singleton with attributes of specified class. At first: Groan. What is it with the Singleton design

Re: Generic singleton

2010-03-04 Thread David Bolen
Duncan Booth duncan.bo...@invalid.invalid writes: It is also *everywhere* in the Python world. Unlike Java and C++, Python even has its own built-in type for singletons. If you want a singleton in Python use a module. So the OP's original examples become: --- file singleton.py --- foo =

Re: Generic singleton

2010-03-04 Thread Steven D'Aprano
On Thu, 04 Mar 2010 12:21:26 +, Duncan Booth wrote: Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Wed, 03 Mar 2010 19:54:52 +0100, mk wrote: Hello, So I set out to write generic singleton, i.e. the one that would do a singleton with attributes of specified class

Re: Generic singleton

2010-03-04 Thread Steve Howell
On Mar 4, 7:32 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: Python does have it's own singletons, like None, True and False. For some reason, they behave quite differently: NoneType fails if you try to instantiate it again, while bool returns the appropriate existing

Re: Generic singleton

2010-03-04 Thread Gregory Ewing
mk wrote: Or I could make my life simpler and use global variable. :-) Indeed. You actually *have* a global variable already, you've just hidden it inside another object. That doesn't make it any less global, though. If you want to defer creation of the object until the first time it's used,

Generic singleton

2010-03-03 Thread mk
Hello, So I set out to write generic singleton, i.e. the one that would do a singleton with attributes of specified class. At first: class Singleton(object): instance = None def __new__(cls, impclass, *args, **kwargs): if cls.instance is None: cls.instance

Re: Generic singleton

2010-03-03 Thread Arnaud Delobelle
mk mrk...@gmail.com writes: [...] hashable .. All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Well ok, hashable they're not; but apparently at least dict and list have id()? lists and dicts are not hashable, but

Re: Generic singleton

2010-03-03 Thread mk
Arnaud Delobelle wrote: mk mrk...@gmail.com writes: [...] hashable .. All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Well ok, hashable they're not; but apparently at least dict and list have id()? lists and dicts

Re: Generic singleton

2010-03-03 Thread Jonathan Gardner
On Wed, Mar 3, 2010 at 1:11 PM, mk mrk...@gmail.com wrote: Or I could make my life simpler and use global variable. :-) Ding ding ding! 90% of Design Patterns is making Java suck less. Other languages don't necessarily suffer from Java's design flaws. -- Jonathan Gardner

Re: Generic singleton

2010-03-03 Thread Bruno Desthuilliers
mk a écrit : does every builtin class have unique id? Classes are objects. And every object *within a python process* has it's own unique id. For a definition of unique being unique amongst the objects living in the process at a given time - IOW, if an object is garbage-collected, it's id can

Re: Generic singleton

2010-03-03 Thread Steven D'Aprano
On Wed, 03 Mar 2010 19:54:52 +0100, mk wrote: Hello, So I set out to write generic singleton, i.e. the one that would do a singleton with attributes of specified class. At first: Groan. What is it with the Singleton design pattern? It is one of the least useful design patterns, and yet

Re: Generic singleton

2010-03-03 Thread Jack Diederich
On Wed, Mar 3, 2010 at 5:18 PM, Jonathan Gardner jgard...@jonathangardner.net wrote: On Wed, Mar 3, 2010 at 1:11 PM, mk mrk...@gmail.com wrote: Or I could make my life simpler and use global variable. :-) Ding ding ding! 90% of Design Patterns is making Java suck less. Other languages