in-module global variables

Simon Brunning SBrunning at
Thu Sep 28 12:27:03 CEST 2000

> :matb at]
> When writing a python module, how do I correctly declare a global
> variable-
> i.e. a variable that has scope throughout the whole of the module?
>From the Python Reference Manual:

6.12 The global statement 

global_stmt:    "global" identifier ("," identifier)*

The global statement is a declaration which holds for the entire current
code block. It means that the listed identifiers are to be interpreted as
globals. While using global names is automatic if they are not defined in
the local scope, assigning to global names would be impossible without

Names listed in a global statement must not be used in the same code block
textually preceding that global statement. 

Names listed in a global statement must not be defined as formal parameters
or in a for loop control target, class definition, function definition, or
import statement. 

(The current implementation does not enforce the latter two restrictions,
but programs should not abuse this freedom, as future implementations may
enforce them or silently change the meaning of the program.) 

Programmer's note: the global is a directive to the parser. It applies only
to code parsed at the same time as the global statement. In particular, a
global statement contained in an exec statement does not affect the code
block containing the exec statement, and code contained in an exec statement
is unaffected by global statements in the code containing the exec
statement. The same applies to the eval(), execfile() and compile()

The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.

More information about the Python-list mailing list