[Tutor] Global var not defined?

Steven D'Aprano steve at pearwood.info
Wed Aug 28 01:32:01 CEST 2013


On 28/08/13 03:58, leam hall wrote:
> Could use some help with this. Python 2.4.3 on RHEL 5.x.
>
> In the functions file that gets imported:
>
> def append_customer(line_list):
>          global customers

Globals are not "globally global", they are global to the module. Otherwise variables defined in module X would stomp all over variables defined in module Y, unpredictably depending on the order than modules were imported.


> In the calling file:
>
> import functions
> import sys
>
> customers = []


This cannot work, because it belongs to a different namespace (module). Just pass customers as an explicit parameter to append_customer, and then google for "Global variables considered harmful".

If you absolutely must emulate COBOL programmers of the 1970s and insist on using globals, write:

functions.customers = []

instead.



-- 
Steven


More information about the Tutor mailing list