[Tutor] Global scope

Bob Gailer ramrom@earthling.net
Sat Feb 1 16:50:02 2003


--=======7D624F9E=======
Content-Type: text/plain; x-avg-checked=avg-ok-72668EC; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

This is probably NOT the best solution, but here's how you can "peek" at 
the main module. In myFunc1 put:

import sys
main = sys.modules['__main__']
print main.a

You can do anything you want with main.a (as long as its legal in Python).

This technique also lets you reference anything in any imported module.

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

--=======7D624F9E=======
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

--=======7D624F9E=======--