singleton ... again

Roy Smith roy at panix.com
Wed Feb 12 06:55:59 CET 2014


In article <mailman.6728.1392183929.18130.python-list at python.org>,
 Dave Angel <davea at davea.name> wrote:

>  Asaf Las <roegltd at gmail.com> 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 myclass.class_instance == None:
> >             return object.__new__(cls)
> >         return myclass.class_instance
> > 
> >     def __init__(self, some):
> >         if self.__class__.class_instance == None: # init blocker 
> >             self.__class__.class_instance = self
> >             self.member = some
> >     
> >     def __del__(self):
> >         self.__class__.class_instance = None
> >     
> > one_class = myclass(1)
> > print(id(one_class), one_class.member )
> > 
> > two_class = myclass(2)
> > print(id(two_class), two_class.member)
> > 
> > 
> 
> Perhaps if you would state your actual goal,  we could judge
>  whether this code is an effective way to accomplish
>  it.

It looks to me like he's trying to implement a classic Gang of Four 
singleton pattern.



More information about the Python-list mailing list