singleton problems

harryos oswald.harry at gmail.com
Sun Oct 3 06:13:42 EDT 2010


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 self.
>
>
> Try this instead:
>
> class Singleton(object):
>     _instance = None
>     def __new__(cls, *args, **kwargs):
>         if not cls._instance:
>             cls._instance = super(Singleton, self).__new__(cls)
>             # or if you prefer: object.__new__(cls)
>         return cls._instance
>



More information about the Python-list mailing list