Injecting a global into a defined function??

Terry Reedy tjreedy at
Fri Jan 16 08:48:26 CET 2009

Cong Ma wrote:
> Terry Reedy wrote:
>> Not to me.  You are using the module as a singleton class. The
>> alternative is to write a class, make the functions methods, and
>> instantiate the class.  If that instance must be a singleton, more work
>> is required.  If multiple instances make sense, you can go the class
>> route when you need to.
> I've thought of this too, but it turns out those functions are related to each
> other very loosely. They do a lot of completely different things independently
> and have just one common global variable to share. IMHO it would reduce the
> readability of code because the role of this class is very unclear. Anyway,
> "readability" is mostly a subjective matter and I think what you pointed out is
> a good idea in general.

 From a user perspective, there is little difference between

import mod
<use mod.f1, mod.f2, etc>


from mod import mod_class_singleton_instance as mod
<use mod.f1, mod.f2, etc>

Modules and classes are both namespaces with names accessed as 
attributes (dotted names) rather than as quoted keys.  (Classes are more 

More information about the Python-list mailing list