[Tutor] Global scope

Pete Versteegen pversteegen@gcnetmail.net
Sat Feb 1 19:44:03 2003


Hi,

I suppose one solution is to write a program that combines all of the
individual modules into a single module so that it can access the global
variable/list space.



Pete Versteegen
pversteegen@gcnetmail.net
__________________________





On 2/1/03 4:31 PM, "Bob Gailer" <ramrom@earthling.net> wrote:

> The scope of globals is the containing module. Thus "global case_number"
> wants case_number to be in module myFunc1. It will NOT look anywhere else.
> 
> Importing is hierarchical; main can import other modules, and can reference
> objects therein; the imported modules can't see objects in the main module.
> 
> Please tell us a bit more about what you want to accomplish, then we can
> suggest alternatives.
> 
> At 01:42 PM 2/1/2003 -0500, Pete Versteegen wrote:
> 
>> It's not working.... Here is what I'm doing:
>> 
>> The following code is saved in a file named main.py
>> 
>> #!/usr/bin/python
>> 
>> from myFunc1 import myFunc1
>> 
>> global case_number
>> case_number = 0
>> 
>> last_case = myFunc1()
>> 
>> print case_number
>> 
>> 
>> 
>> The following code is saved in a file named myFunc1.py
>> 
>> def myFunc1():
>>      global case_number
>>      case_number += 1
>> 
>> 
>> When I execute main: ./main.py, I get:
>> 
>> Traceback (most recent call last):
>>   File "./main.py", line 8, in ?
>>     last_case = myFunc1()
>>   File "./myFunc1.py", line 3, in myFunc1
>>     case_number += 1
>> NameError: global name 'case_number' is not defined
>> 
>> 
>> 
>> 
>> Pete Versteegen
>> pversteegen@gcnetmail.net
>> __________________________
>> 
>> 
>> 
>> _______________________________________________
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
>> ---
>> Incoming mail is certified Virus Free.
>> Checked by AVG anti-virus system (http://www.grisoft.com).
>> Version: 6.0.445 / Virus Database: 250 - Release Date: 1/21/2003
> 
> Bob Gailer
> mailto:ramrom@earthling.net
> 303 442 2625
> 
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.445 / Virus Database: 250 - Release Date: 1/21/2003
>