best way to share an instance of a class among modules?

Michael Torrie torriem at gmail.com
Fri Feb 8 23:54:23 EST 2013


On 02/07/2013 07:14 PM, Rick Johnson wrote:

> So if you want to use "global variables" , (bka: Module level
> variables), then simply declare them with a None value like this:
> 
> globalVariable = None

This is a nice convention, but at best it's just a helpful notation that
helps a programmer know something useful may likely be bound to this
name in the future.

The '=' sign in Python isn't an assignment operator, at least in this
case; it's a binding operator.  Thus any future line that sets
globalVariable to something isn't assigning a value to this
already-declared variable.  Instead it's binding the name globalValue to
a new object, and overwriting the name in the module dictionary.  That's
of course why the global keyword is required, so that Python will use
the module dictionary instead of the local scope one.



More information about the Python-list mailing list