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

Re: SIngleton from __defaults__

2014-01-24 Thread Johannes Schneider
thnx guys. On 24.01.2014 01:10, Terry Reedy wrote: Johannes Schneider Wrote in message: On 22.01.2014 20:18, Ned Batchelder wrote: On 1/22/14 11:37 AM, Asaf Las wrote: Chris is right here, too: modules are themselves singletons, no matter how many times you import them, they are only exec

Re: SIngleton from __defaults__

2014-01-23 Thread Terry Reedy
Johannes Schneider Wrote in message: On 22.01.2014 20:18, Ned Batchelder wrote: On 1/22/14 11:37 AM, Asaf Las wrote: Chris is right here, too: modules are themselves singletons, no matter how many times you import them, they are only executed once, and the same module object is provided for

Re: SIngleton from __defaults__

2014-01-23 Thread Dave Angel
Johannes Schneider Wrote in message: > On 22.01.2014 20:18, Ned Batchelder wrote: >> On 1/22/14 11:37 AM, Asaf Las wrote: >> Chris is right here, too: modules are themselves singletons, no matter >> how many times you import them, they are only executed once, and the >> same module object is pro

Re: SIngleton from __defaults__

2014-01-23 Thread Johannes Schneider
On 22.01.2014 20:18, Ned Batchelder wrote: On 1/22/14 11:37 AM, Asaf Las wrote: Chris is right here, too: modules are themselves singletons, no matter how many times you import them, they are only executed once, and the same module object is provided for each import. I'm not sure, if this is th

Re: SIngleton from __defaults__

2014-01-22 Thread Asaf Las
On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: > ChrisA and this one is about multiclass container function with multithreading support: import threading def provider(cls, x = [threading.Lock(), {}]): provider.__def

Re: SIngleton from __defaults__

2014-01-22 Thread Asaf Las
On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: > > is it possible to create singleton using construct below : > > > > def singleton_provider(x = [None]): > > if singleton_provider.__defaults__[0][0] == None: > >

Re: SIngleton from __defaults__

2014-01-22 Thread Asaf Las
On Wednesday, January 22, 2014 9:18:19 PM UTC+2, Ned Batchelder wrote: > Chris is right here, too: modules are themselves singletons, no matter > how many times you import them, they are only executed once, and the > same module object is provided for each import. > > Ned Batchelder, http://nedb

Re: SIngleton from __defaults__

2014-01-22 Thread Ned Batchelder
On 1/22/14 11:37 AM, Asaf Las wrote: On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: Why not simply: def get_singleton(x = SomeClass()): return x Or even: singleton = SomeClass() ? Neither of the above provides anythi

Re: SIngleton from __defaults__

2014-01-22 Thread 88888 Dihedral
On Thursday, January 23, 2014 12:37:36 AM UTC+8, Asaf Las wrote: > On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: > > > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: > > > > > > Why not simply: > > > def get_singleton(x = SomeClass()): > > > return x > > > Or

Re: SIngleton from __defaults__

2014-01-22 Thread Asaf Las
On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: > > Why not simply: > def get_singleton(x = SomeClass()): > return x > Or even: > singleton = SomeClass() > ? Neither of the above provides anything above the last one, ex

Re: SIngleton from __defaults__

2014-01-22 Thread Chris Angelico
On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las wrote: > is it possible to create singleton using construct below : > > def singleton_provider(x = [None]): > if singleton_provider.__defaults__[0][0] == None: > singleton_provider.__defaults__[0][0] = SomeClass() > return singleton_provide

Re: singleton problems

2010-10-03 Thread Bruno Desthuilliers
jimgardener a écrit : > hi Steven, > can you explain that?I didn't quite get it. > I have a module say 'managerutils' where I have a class > MyManager.. What Steven was talking about was to NOT use a class at all. Modules are objects and have their own namespace. And you can use threading.locals i

Re: singleton problems

2010-10-03 Thread harryos
thanks Arnold..that made it quite clear harry On Oct 3, 4:11 pm, Arnaud Delobelle wrote: > Arnaud Delobelle writes: > -- http://mail.python.org/mailman/listinfo/python-list

Re: singleton problems

2010-10-03 Thread Arnaud Delobelle
Arnaud Delobelle writes: [...] > That's because overriding __new__ doesn't prevent __init__ from being > executed. The reason for this is that when you do: > > MySingle('jeff') > > what is executed is: > > MySingle.__metaclass__.__call__('jeff') Oops. I meant: MySingle.__metaclass_

Re: singleton problems

2010-10-03 Thread Arnaud Delobelle
harryos writes: > hi > I have been trying out singleton design pattern implementations..I > wrote this, > > > class Singleton(object): > _instance = None > def __new__(self, *args, **kwargs): > if not self._instance: > self._instance = super(Singleton, self).__new__(se

Re: singleton problems

2010-10-03 Thread jimgardener
hi Steven, can you explain that?I didn't quite get it. I have a module say 'managerutils' where I have a class MyManager.. ie, managerutils.py -- class MyManager(object): def __init__(self): self.myaddresses={} ... from another main program ,if I call , import managerutils

Re: singleton problems

2010-10-03 Thread harryos
thanks Steven..that was very helpful..thanks a lot harry > Since __new__ is called before the instance exists, it doesn't receive an > instance as the first argument. Instead it receives the class. While you > can call the parameter anything you like, it is conventional to call it > cls rather than

Re: singleton problems

2010-10-03 Thread Steven D'Aprano
On Sun, 03 Oct 2010 01:55:00 -0700, harryos wrote: > hi > I have been trying out singleton design pattern implementations..I wrote > this, > > > class Singleton(object): > _instance = None > def __new__(self, *args, **kwargs): > if not self._instance: > self._instance

Re: Singleton implementation problems

2008-07-05 Thread Urizev
Great! Thanks everyone for so many references and comments. Lots of doubts have been solved. On Fri, Jul 4, 2008 at 10:33 AM, Peter Otten <[EMAIL PROTECTED]> wrote: > Ben Finney wrote: > >> Peter Otten <[EMAIL PROTECTED]> writes: >> >>> The problem is the structure of your program. The myset modul

Re: Singleton implementation problems

2008-07-04 Thread Terry Reedy
Ben Finney wrote: Peter Otten <[EMAIL PROTECTED]> writes: The problem is the structure of your program. The myset module is imported twice by Python, once as "myset" and once as "__main__". Yes, this is the problem. Each module imports the other. Therefore you get two distinct MySet class

Re: Singleton implementation problems

2008-07-04 Thread Peter Otten
Ben Finney wrote: > Peter Otten <[EMAIL PROTECTED]> writes: > >> The problem is the structure of your program. The myset module is >> imported twice by Python, once as "myset" and once as "__main__". > > Yes, this is the problem. Each module imports the other. > >> Therefore you get two distinc

Re: Singleton implementation problems

2008-07-04 Thread Matthew Fitzgibbons
Ben Finney wrote: Peter Otten <[EMAIL PROTECTED]> writes: The problem is the structure of your program. The myset module is imported twice by Python, once as "myset" and once as "__main__". Yes, this is the problem. Each module imports the other. Therefore you get two distinct MySet classes

Re: Singleton implementation problems

2008-07-04 Thread Ben Finney
Peter Otten <[EMAIL PROTECTED]> writes: > The problem is the structure of your program. The myset module is > imported twice by Python, once as "myset" and once as "__main__". Yes, this is the problem. Each module imports the other. > Therefore you get two distinct MySet classes, and consequentl

Re: Singleton implementation problems

2008-07-03 Thread Peter Otten
Urizev wrote: > Hi everyone > > I have developed the singleton implementation. However I have found a > strange behaviour when using from different files. The code is > attached. > > Executing main > New singleton: > <__main__.Singleton instance at 0x2b98be474a70> > New singleton: > > I do n

Re: Singleton implementation problems

2008-07-03 Thread George Sakkis
On Jul 3, 6:58 pm, Urizev <[EMAIL PROTECTED]> wrote: > Hi everyone > > I have developed the singleton implementation. However I have found a > strange behaviour when using from different files. The code is > attached. > > Executing main > new MySet object > No singleton instance > New singleton: >

Re: Singleton implementation problems

2008-07-03 Thread Matthew Fitzgibbons
Urizev wrote: Hi everyone I have developed the singleton implementation. However I have found a strange behaviour when using from different files. The code is attached. Executing main new MySet object No singleton instance New singleton: <__main__.Singleton instance at 0x2b98be474a70> new MySet

Re: singleton decorator

2008-03-27 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > Hallo, > playing with the decorators from PEP 318 I found the elegant singleton > decorator. > > def singleton(cls): > instances = {} > def getinstance(): > if cls not in instances: > instances[cls] = cls() > return instances[cls] >

Re: Singleton

2007-10-16 Thread pythoncurious
Hi again, Just wanted to say thanks. I now see that I misunderstood where the problem was and I got useful information about how importing stuff works. My problem is solved and I'm grateful for your help. -- http://mail.python.org/mailman/listinfo/python-list

Re: Singleton

2007-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2007 17:37:53 +0200, Diez B. Roggisch wrote: >> Two borgs, or two million, the whole point of using borgs is that it >> doesn't matter. > > It does matter where the classes live (module-wise). Which is the > problem the OP had, borg or not to borg. Gotcha. Thanks for the demonstr

Re: Singleton

2007-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2007 09:52:26 -0700, Carl Banks wrote: > On Oct 10, 11:18 am, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> Of course, if there is some other meaning to the OP's post, then >> possibly I've completely misunderstood it. Would you mind telling me >> what you've se

Re: Singleton

2007-10-10 Thread Carl Banks
On Oct 10, 11:18 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > Of course, if there is some other meaning to the OP's post, then possibly > I've completely misunderstood it. Would you mind telling me what you've > seen that the rest of us haven't? I read far enough to see that

Re: Singleton

2007-10-10 Thread Diez B. Roggisch
Steven D'Aprano wrote: > On Wed, 10 Oct 2007 06:57:15 -0700, Carl Banks wrote: > >> On Oct 10, 9:39 am, Steven D'Aprano <[EMAIL PROTECTED] >> cybersource.com.au> wrote: >>> On Wed, 10 Oct 2007 09:36:56 +, pythoncurious wrote: >>> > So how do people solve this? Is there an obvious way that I m

Re: Singleton

2007-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2007 06:57:15 -0700, Carl Banks wrote: > On Oct 10, 9:39 am, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> On Wed, 10 Oct 2007 09:36:56 +, pythoncurious wrote: >> > So how do people solve this? Is there an obvious way that I missed? >> >> Mostly by avoiding

Re: Singleton

2007-10-10 Thread Hrvoje Niksic
[EMAIL PROTECTED] writes: > Now when I run the 'run.py', it will print two different numbers. > sys.modules tells me that 'mod1' is imported as both 'one.mod1' and > 'mod1', which explains the result. If I were you, I'd make sure that the module duplicate problem is resolved first, for example by

Re: Singleton

2007-10-10 Thread Carl Banks
On Oct 10, 9:39 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Wed, 10 Oct 2007 09:36:56 +, pythoncurious wrote: > > So how do people solve this? Is there an obvious way that I missed? > > Mostly by avoiding singletons. Why do you need only one instance? Perhaps > you sh

Re: Singleton

2007-10-10 Thread Steven D'Aprano
On Wed, 10 Oct 2007 09:36:56 +, pythoncurious wrote: > So how do people solve this? Is there an obvious way that I missed? Mostly by avoiding singletons. Why do you need only one instance? Perhaps you should consider the Borg pattern instead. http://aspn.activestate.com/ASPN/Cookbook/Python

Re: Singleton

2007-10-10 Thread Carl Banks
On Oct 10, 9:34 am, [EMAIL PROTECTED] wrote: > Spring Python (http://springpython.python-hosting.com) offers a [snip spam] Too bad your bot can't be bothered to read the post it would have known the singleton wasn't the problem here. Carl Banks -- http://mail.python.org/mailman/listinfo/pytho

Re: Singleton

2007-10-10 Thread OnCallSupport321
Spring Python (http://springpython.python-hosting.com) offers a singleton solution. You can mark up function calls to only be called the first time, i.e. as singletons. The results are cached inside the Spring Python IoC container, so the next time the function is called, it returns the cached copy

Re: Singleton

2007-10-10 Thread Carl Banks
On Oct 10, 5:36 am, [EMAIL PROTECTED] wrote: > Hi, > > I've been trying to get some sort of singleton working in python, but > I struggle a bit and I thought I'd ask for advice. > > The first approach was simply to use a module, and every variable in > it will be seen by all who import the module.

Re: Singleton in Python Cookbook

2007-07-26 Thread Alex Popescu
Steve Holden <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > Alex Popescu wrote: >> Alex Popescu <[EMAIL PROTECTED]> wrote in >> news:[EMAIL PROTECTED]: >> >>> "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in >>> news:[EMAIL PROTECTED]: >>> > > [snip...] > > If, that is, "work" means "R

Re: Singleton in Python Cookbook

2007-07-26 Thread Bruno Desthuilliers
Alex Popescu a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > >> Alex Popescu a écrit : > >> [snip...] >> >> >> I don't have the book, so if you don't post the code, I just give up >> trying to guess what the problem can be. > > I've sent the original co

Re: Singleton in Python Cookbook

2007-07-26 Thread Alex Popescu
Bruno Desthuilliers <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > Alex Popescu a écrit : > > [snip...] > > > I don't have the book, so if you don't post the code, I just give up > trying to guess what the problem can be. I've sent the original code and 2 different variants a long tim

Re: Singleton in Python Cookbook

2007-07-26 Thread Bruno Desthuilliers
Alex Popescu a écrit : > Hi all! > > I was reading through Python Cookbook the Singleton recipe. At this moment > I am a bit puzzled as the example in the book is not working resulting in: > > TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type > > (I haven't presented the

Re: Singleton in Python Cookbook

2007-07-25 Thread Steve Holden
Alex Popescu wrote: > Alex Popescu <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > >> "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in >> news:[EMAIL PROTECTED]: >> >>> Alex Popescu schrieb: Hi all! I was reading through Python Cookbook the Singleton recipe. At this m

Re: Singleton in Python Cookbook

2007-07-25 Thread O.R.Senthil Kumaran
* Alex Popescu <[EMAIL PROTECTED]> [2007-07-25 21:30:14]: > TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type That's hardly helpful. All I can think about is, SingleSpam is a class and you have to defined the Class in your program, or it could be something else also. > (I

Re: Singleton in Python Cookbook

2007-07-25 Thread John J. Lee
Alex Popescu <[EMAIL PROTECTED]> writes: > Hi all! > > I was reading through Python Cookbook the Singleton recipe. At this moment > I am a bit puzzled as the example in the book is not working resulting in: > > TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type Haven't look

Re: Singleton in Python Cookbook

2007-07-25 Thread John J. Lee
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: [...] > AFAIK the cookbook is completely found online at ASPN. So no sweat > publishing it here. [...] No: the book-form cookbook is edited, and has extra text. I believe the recipes are under a BSD-style license, though. John -- http://mail.pytho

Re: Singleton in Python Cookbook

2007-07-25 Thread Alex Popescu
Alex Popescu <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > >> Alex Popescu schrieb: >>> Hi all! >>> >>> I was reading through Python Cookbook the Singleton recipe. At this >>> moment I am a bit puzzled as the

Re: Singleton in Python Cookbook

2007-07-25 Thread Alex Popescu
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > Alex Popescu schrieb: >> Hi all! >> >> I was reading through Python Cookbook the Singleton recipe. At this >> moment I am a bit puzzled as the example in the book is not working >> resulting in: >> >> TypeError: type.__n

Re: Singleton in Python Cookbook

2007-07-25 Thread Diez B. Roggisch
Alex Popescu schrieb: > Hi all! > > I was reading through Python Cookbook the Singleton recipe. At this moment > I am a bit puzzled as the example in the book is not working resulting in: > > TypeError: type.__new__(SingleSpam): SingleSpam is not a subtype of type > > (I haven't presented the o

RE: Singleton Class Exception

2006-11-13 Thread Ames Andreas
> -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > g] On Behalf Of Dan Lenski > Sent: Monday, November 13, 2006 7:05 PM > Subject: Re: Singleton Class Exception > > dischdennis wrote: > > the line "raise Singleton.__single&qu

Re: Singleton Class Exception

2006-11-13 Thread Jason
I threw together two different singleton classes. They both ensure that the initializers and destructors can be called at most once. (The ImmortalSingleton won't ever have its destructor called unless the programmer manually forces things.) I haven't extensively tested these things, so handle th

Re: Singleton Class Exception

2006-11-13 Thread Dan Lenski
dischdennis wrote: > the line "raise Singleton.__single" invokes in my class the following > error: > > exceptions must be classes, instances, or strings (deprecated), not > PurchaseRequisitionController Denis, Jason's explanation is correct! You are trying to use the Singleton instance as the ex

Re: Singleton Class Exception

2006-11-13 Thread Jason
dischdennis wrote: > Hello List, > > I would like to make a singleton class in python 2.4.3, I found this > pattern in the web: > > class Singleton: > __single = None > def __init__( self ): > if Singleton.__single: > raise Singleton.__single > Singleton.__single

Re: singleton decorator

2006-08-09 Thread Pedro Werneck
On Tue, 08 Aug 2006 14:50:39 +0200 Wildemar Wildenburger <[EMAIL PROTECTED]> wrote: > Or you could always just use the __new__() method instead of > __init__(), putting all your initialization into the above > except-block. If you replace 'cls._it = ...' with 'self = cls_it = > ...' you'll feel

Re: singleton decorator

2006-08-09 Thread Wildemar Wildenburger
Pedro Werneck wrote: class Singleton(object): > ... def __new__(cls, *args, **kwds): > ... try: > ... return cls._it > ... except AttributeError: > ... cls._it = object.__new__(cls, *args, **kwds) > ... ret

Re: singleton decorator

2006-08-08 Thread Paddy
Andre Meyer wrote: > Am I missing something here? What is the preferred pythonic way of > implementing singleton elegantly? > > Thanks for your help > André Hi Andre, You might also google for python borg pattern As a discussion on the 'borg' design pattern might be informative. - Pad. -- ht

Re: singleton decorator

2006-08-08 Thread Georg Brandl
Andre Meyer wrote: > While looking for an elegant implementation of the singleton design > pattern I came across the decorator as described in PEP318 > . > Unfortunately, the following does not work, because decorators only work > on functions or methods

Re: singleton decorator

2006-08-08 Thread Chaz Ginger
[EMAIL PROTECTED] wrote: > Andre Meyer: >> What is the preferred pythonic way of implementing singleton elegantly? > > Maybe to just use a module. > > Bye, > bearophile > Here is some sample code for both singleton classes and named classes that I use: > class Singleton(type): > """ >

Re: singleton decorator

2006-08-07 Thread Pedro Werneck
On Tue, 8 Aug 2006 01:33:31 +0200 "Andre Meyer" <[EMAIL PROTECTED]> wrote: > > Am I missing something here? What is the preferred pythonic way of > implementing singleton elegantly? I think the most "elegant" is with a metaclass, since I feel like a singleton is not just an ordinary type (and __

Re: singleton decorator

2006-08-07 Thread Erik Max Francis
Andre Meyer wrote: > Am I missing something here? What is the preferred pythonic way of > implementing singleton elegantly? Create a class and then derive from it. There are examples on the Cookbook. -- Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/ San Jose, CA, USA &

Re: singleton decorator

2006-08-07 Thread bearophileHUGS
Andre Meyer: > What is the preferred pythonic way of implementing singleton elegantly? Maybe to just use a module. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

  1   2   >