singleton ... again

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Feb 12 22:57:02 CET 2014


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 able to use the object
constructor, instead of just using the prebuilt
instance?

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 time it's called. That's a pattern that
*is* useful, and I've often used in Python and
other languages.

E.g.

_the_whatsit = None

def get_whatsit():
    if _the_whatsit is None:
       _the_whatsit = Whatsit()
    return _the_whatsit

-- 
Greg



More information about the Python-list mailing list