[Tutor] Global scope
Bob Gailer
ramrom@earthling.net
Sat Feb 1 16:34:01 2003
--=======44DA7583=======
Content-Type: text/plain; x-avg-checked=avg-ok-72668EC; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit
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
--=======44DA7583=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-72668EC
Content-Disposition: inline
---
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
--=======44DA7583=======--