__new__() does not return anything, on singletong pattern

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Mar 12 04:38:00 EDT 2015


Mario Figueiredo wrote:

> It's just a cheap global, since is ubiquitous throughout the entire
> application, does behave like a singleton, and is a bit too expensive
> to create. A full map in the main application takes 3 or 4 seconds to
> instantiate and occupies around 2 Mb of memory.

There's nothing wrong with having only one instance. The
quesion is whether it's a good idea to make calling Map()
be the way to get hold of that instance.

I would say it's counterproductive. The implementation
is convoluted, and it makes code that calls Map()
confusing, because it looks like it's creating a new
instance when it really isn't.

I would just provide a function:

_map = None

def get_map():
    global _map
    if _map is None:
       _map = Map()
    return _map

and document the fact that you shouldn't call Map()
directly.

-- 
Greg



More information about the Python-list mailing list