[Tutor] Query - Where to put in global variables, if needed, as a good programming practice

Walter Prins wprins at gmail.com
Fri Jun 15 14:48:58 CEST 2012

Hi Spawgi,

On 15 June 2012 12:44,  <spawgi at gmail.com> wrote:
> Hello,
> The point of good-bad-ness of global variables aside, if I needed to use
> them, which is a better place to put them.
> 1. In the __init__ function of a class? So they are available at the time an
> object is initialized or

Firstly note as a bit of an aside that member variables of an object
(or perhaps of the class), are not considered global, at least not in
the sense most people mean when they talk about "global" variables.

I'm however assuming you're NOT referring to a variable that's
actually either a class or object member variable in your question,
but rather something akin to a true global.

I say "akin", because Python does not really have true global
variables in the sense that one might expect coming from some other
langauges.  The closest is probably the contents of the '__builtin__'
module, which is the last searched when resolving names and hence
practically speaking probably the closest to containing what one might
describe as global variables, but I'd hazard to submit there will
virtually without exception always be a better location for your
variables than to plug them into the __builtin__ module.

> 2. In the actual function of the class where the variables are needed?
> Pros and Cons of either approach?

OK, to state the obvious again, but if a variable is only needed in
one method (function) of a class, then by definition you don't
actually need a global.  If it's needed in more than one method in an
object, then you should probably consider an object member variable or
suitably parameterizethe methods.

That aside, to answer your question in a general fashion,
initialisation should always happen in the initialization section (as
appropriate) of the namespace that a variable is being
declared/contained in.  So package level varibles should be
initialized in __init__.py of the package, module variables should be
initialized in the module itself, prior to any other code that may
refer to them obviously, and presumably towards the top of the module
in most cases, class level variables should be initialized directly in
the class prior to use, object level variables in the object
initializer (__init__ method) and so on.

So, your (presumably module) global variables should therefore *not*
be initialized as part of the object initializer (what happens when
you create multiple instances of your object?), object methods (what
happens when you call the method multiple times?) or even plain
functions (again, what happens when you call the method multiple
times?) as you suggested, and assuming you're talking about module
globals, they should in fact simply be initialized in the module,
prior to use elsewhere in the module.  (Use from other modules are
possible by importing the module containing the module global variable
from such other modules etc.)


More information about the Tutor mailing list