June 2002
Beginner to intermediate
640 pages
14h 24m
English
The word “global” strikes fear in the hearts of object-oriented programmers (and structured programmers, for that matter). That's because global variables—variables that are in a global namespace (one that's available to all modules, functions, and classes)—are usually a bad thing and should be avoided.
In fact, there aren't any real global variables in Python. All variables have to be in the context of a module, class, or function. However, Python does have a global statement, which allows you to share variables between functions in the same module.
Here's an example that has two functions and a module-level variable:
var = "hi" #module level variable def sayHi1(): print var def sayHi2(): print var def sayHiMom(): ...
Read now
Unlock full access