newbie - Scoping question
P Adhia
padhia at yahoo.com
Mon Sep 17 11:59:13 EDT 2001
Hello,
I'll appreciate if anyone can me clarify my Python scoping rules a
bit.
Consider following code in modlue test.py
----- code begin ----
"""Module test.py"""
x = []
def get_x():
print x
def set_x(default):
global x
x = default
----- code end ----
now I am trying to set value of "x" interactively as,
----- python interactive session begin ----
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import test
>>> from test import *
>>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', 'get_x', 'set_x',
'x']
>>> x = [1,2,3]
>>> x
[1, 2, 3]
>>> get_x()
[]
>>> test.x = [4,5,6]
>>> get_x()
[4, 5, 6]
>>> x
[1, 2, 3]
>>>
----- python interactive session end ----
I am confused as to why "x", when not qualified with module name, does
not refer to "x" in "test", even though I did "from test import *".
TIA
P. Adhia
More information about the Python-list
mailing list