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() >

singleton decorator

2008-03-27 Thread r . grimm
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] return getinstance @singleton class A: pass

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

Re: singleton decorator

2006-08-07 Thread Farshid Lashkari
Andre Meyer wrote: > Am I missing something here? What is the preferred pythonic way of > implementing singleton elegantly? The "Open Issues" section of that PEP says the following: "It's exceedingly unlikely that class decorators will be in Python 2.4" So it might not be in the current version

singleton decorator

2006-08-07 Thread Andre Meyer
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, but not on classes. def singleton(cls):instances = {}def getinstan