Re: singleton ... again

2014-02-13 Thread Gregory Ewing
Ethan Furman wrote: 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). No, you shouldn't ask for SerialPort(2) at all, you should call get_serial_port(2). Then you won't be fooled into thinking that you're cr

Re: singleton ... again

2014-02-13 Thread Gregory Ewing
Steven D'Aprano wrote: Of course it can happen by accident. It's happened to me, where I've accidentally called NoneType() (which raises, rather than returning a new instance). Well, "unlikely to happen by accident", then. -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: singleton ... again

2014-02-13 Thread Gregory Ewing
Steven D'Aprano wrote: On Wed, 12 Feb 2014 23:04:32 +1300, Gregory Ewing wrote: If you really want to make sure nobody creates another instance by accident, delete the class out of the namespace after instantiating it. That does not work. It is trivial to get the type from an instance: I sa

Re: singleton ... again

2014-02-13 Thread Grant Edwards
On 2014-02-13, Roy Smith wrote: > I envision SerialPort being a thin layer on top of a bunch of > OS-specific system calls to give them a pythonic interface. Yep, that's pretty much what pyserial is http://pyserial.sourceforge.net/ > Things like is_shutdown() and set_bit_rate() presumably t

Re: singleton ... again

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:13 AM, Robert Kern wrote: > We don't use `is None` instead of `== None` for the speed. We use it for > robustness. We don't want arbitrary __eq__()s to interfere with our sentinel > tests. If None weren't a singleton that we could use as such a sentinel, > we'd make one.

Re: singleton ... again

2014-02-13 Thread Robert Kern
On 2014-02-13 20:03, Chris Angelico wrote: On Fri, Feb 14, 2014 at 2:24 AM, Roy Smith wrote: In article , Chris Angelico wrote: On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder wrote: I still don't see it. To convince me that a singleton class makes sense, you'd have to explain why by v

Re: singleton ... again

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 2:24 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder >> wrote: >> > 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 na

Re: singleton ... again

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 6:03 AM, Roy Smith wrote: > In article , > Ethan Furman wrote: >> >> You mean use the Borg pattern instead of the Singleton pattern? As far as I >> can tell they are two shades of the same >> thing. Are there any drastic differences between the two? Besides one >> havi

Re: singleton ... again

2014-02-13 Thread Tim Delaney
On 13 February 2014 20:00, Piet van Oostrum wrote: > Ben Finney writes: > > 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 singleto

Re: singleton ... again

2014-02-13 Thread Roy Smith
In article , Ethan Furman wrote: > On 02/13/2014 09:57 AM, Roy Smith wrote: > > In article , > > Ethan Furman wrote: > > > >> 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). > > > > Why? Certa

Re: singleton ... again

2014-02-13 Thread Ethan Furman
On 02/13/2014 09:57 AM, Roy Smith wrote: In article , Ethan Furman wrote: 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). Why? Certainly, you should get objects which refer to the same physical port.

Re: singleton ... again

2014-02-13 Thread Roy Smith
In article , Ethan Furman wrote: > 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). Why? Certainly, you should get objects which refer to the same physical port. So: port_a = SerialPort(2) port_b

Re: singleton ... again

2014-02-13 Thread Ethan Furman
On 02/13/2014 03:50 AM, Ned Batchelder wrote: On 2/13/14 4:00 AM, Piet van Oostrum wrote: Ben Finney writes: Gregory Ewing 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,

Re: singleton ... again

2014-02-13 Thread Roy Smith
In article , Chris Angelico wrote: > On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder > wrote: > > 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 th

Re: singleton ... again

2014-02-13 Thread Chris Angelico
On Thu, Feb 13, 2014 at 10:50 PM, Ned Batchelder wrote: > 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. There's a huge difference, btw,

Re: singleton ... again

2014-02-13 Thread Ned Batchelder
On 2/13/14 4:00 AM, Piet van Oostrum wrote: Ben Finney writes: Gregory Ewing 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 cr

Re: singleton ... again

2014-02-13 Thread Piet van Oostrum
Ben Finney writes: > Gregory Ewing 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 c

Re: singleton ... again

2014-02-12 Thread Chris Angelico
On Thu, Feb 13, 2014 at 3:24 PM, Steven D'Aprano wrote: > Of course it can happen by accident. It's happened to me, where I've > accidentally called NoneType() (which raises, rather than returning a new > instance). It does in 2.7, yes, but not in 3.4: >>> type(None)() is None True Definitely p

Re: singleton ... again

2014-02-12 Thread Steven D'Aprano
On Thu, 13 Feb 2014 14:07:55 +1100, Ben Finney wrote: > Steven D'Aprano writes: > >> On Wed, 12 Feb 2014 23:04:32 +1300, Gregory Ewing wrote: >> > If you really want to make sure nobody creates another instance by >> > accident, delete the class out of the namespace after instantiating >> > it.

Re: singleton ... again

2014-02-12 Thread Ben Finney
Steven D'Aprano writes: > On Wed, 12 Feb 2014 23:04:32 +1300, Gregory Ewing wrote: > > If you really want to make sure nobody creates another instance by > > accident, delete the class out of the namespace after instantiating > > it. > > That does not work. It is trivial to get the type from an i

Re: singleton ... again

2014-02-12 Thread Steven D'Aprano
On Wed, 12 Feb 2014 23:04:32 +1300, Gregory Ewing wrote: > 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 th

Re: singleton ... again

2014-02-12 Thread Roy Smith
In article <9785668d-6bea-4382-8a0c-c1258f2e2...@googlegroups.com>, Asaf Las wrote: > On Wednesday, February 12, 2014 7:48:51 AM UTC+2, Dave Angel wrote: > > > > Perhaps if you would state your actual goal, we could judge > > whether this code is an effective way to accomplish > > it. > > Da

Re: singleton ... again

2014-02-12 Thread Asaf Las
On Wednesday, February 12, 2014 11:57:02 PM UTC+2, Gregory Ewing wrote: > > If you want to hide the distinction between using > call syntax and just accessing a global, then > export a function that returns the global instance. > > That function can even lazily create the instance > the first tim

Re: singleton ... again

2014-02-12 Thread Asaf Las
On Wednesday, February 12, 2014 11:34:34 PM UTC+2, Ned Batchelder wrote: > Not all patterns are useful. Just because it's been enshrined in the > GoF patterns book doesn't mean that it's good for Python. Yes, i understand up to some extend usefulness of patterns. i did not read the GoF book. ye

Re: singleton ... again

2014-02-12 Thread Asaf Las
On Wednesday, February 12, 2014 8:57:09 PM UTC+2, Mark Lawrence wrote: > > For more data on python patterns search for > python+patterns+Alex+Martelli. He's forgotten more on the subject than > many people on this list will ever know :) > > My fellow Pythonistas, ask not what our language can

Re: singleton ... again

2014-02-12 Thread Tim Delaney
On 13 February 2014 08:34, Ned Batchelder wrote: > On 2/12/14 12:50 PM, Asaf Las wrote: > >> On Wednesday, February 12, 2014 7:48:51 AM UTC+2, Dave Angel wrote: >> >>> >>> Perhaps if you would state your actual goal, we could judge >>> whether this code is an effective way to accomplish >>>

Re: singleton ... again

2014-02-12 Thread Michael Torrie
On 02/11/2014 09:34 PM, Asaf Las wrote: > playing a bit with subject. > > pros and cons of this approach? did i create bicycle again? :-) I always thought sticking an object in a module is the simplest form of singleton. -- https://mail.python.org/mailman/listinfo/python-list

Re: singleton ... again

2014-02-12 Thread Gregory Ewing
Asaf Las wrote: There is another one. Once object passes through singletonizator there wont be any other object than first one. Then object constructor can freely be used in every place of code. You're still making things far more complicated than they need to be. *Why* do you want to be a

Re: singleton ... again

2014-02-12 Thread Ned Batchelder
On 2/12/14 12:50 PM, Asaf Las wrote: On Wednesday, February 12, 2014 7:48:51 AM UTC+2, Dave Angel wrote: Perhaps if you would state your actual goal, we could judge whether this code is an effective way to accomplish it. DaveA Thanks! There is no specific goal, i am in process of buildi

Re: singleton ... again

2014-02-12 Thread Mark Lawrence
On 12/02/2014 17:50, Asaf Las wrote: On Wednesday, February 12, 2014 7:48:51 AM UTC+2, Dave Angel wrote: Perhaps if you would state your actual goal, we could judge whether this code is an effective way to accomplish it. DaveA Thanks! There is no specific goal, i am in process of buildi

Re: singleton ... again

2014-02-12 Thread Asaf Las
mistake, object constructor - to class constructor -- https://mail.python.org/mailman/listinfo/python-list

Re: singleton ... again

2014-02-12 Thread Asaf Las
There is another one. Once object passes through singletonizator there wont be any other object than first one. Then object constructor can freely be used in every place of code. Curious if there could be any impact and applicability of this to builtin types. p.s. learned today that object

Re: singleton ... again

2014-02-12 Thread Asaf Las
On Wednesday, February 12, 2014 7:48:51 AM UTC+2, Dave Angel wrote: > > Perhaps if you would state your actual goal, we could judge > whether this code is an effective way to accomplish > it. > DaveA Thanks! There is no specific goal, i am in process of building pattern knowledge in python b

Re: singleton ... again

2014-02-12 Thread Roy Smith
In article , Ben Finney wrote: > Gregory Ewing 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 o

Re: singleton ... again

2014-02-12 Thread Ben Finney
Gregory Ewing 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,

Re: singleton ... again

2014-02-12 Thread Gregory Ewing
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

Re: singleton ... again

2014-02-11 Thread Roy Smith
In article , Dave Angel wrote: > Asaf Las Wrote in message: > > playing a bit with subject. > > > > pros and cons of this approach? did i create bicycle again? :-) > > > > class myclass(object): > > class_instance = None > > > > def __new__(cls, *args, **kwargs): > > if

Re: singleton ... again

2014-02-11 Thread Asaf Las
there is error should assign weakref to class static member otherwise __del__ will never be called. -- https://mail.python.org/mailman/listinfo/python-list

singleton ... again

2014-02-11 Thread Asaf Las
playing a bit with subject. pros and cons of this approach? did i create bicycle again? :-) class myclass(object): class_instance = None def __new__(cls, *args, **kwargs): if myclass.class_instance == None: return object.__new__(cls) return myclass.class_