python 不同文件中的变量引用

今晚测试了一下,发现globals()[ ]函数只能引用同一个文件中的全局变量,对于不同文件中的变量就无能为力,

而且,有点奇怪的是对于在函数中引用变量的问题,如果引用的是列表和字典,那么可以在函数中修改它的值,但是对于普通的变量,在函数中无法修改他的值,因为传递给函数的只是一个复制的值

 1 #!/bin/usr/python
 2 import sys
 3 sys.path.append('/home/tcstory/desktop')
 4 import b
 5 test={'a':1}
 6 b.testing(test)
 7 x=4
 8 b.test1(x)
 9 print 'original value is ',x
10 lists=[1,2,3]
11 b.test2(lists)
1 def testing(test):
2     del test['a']
3     print test
4 def test1(x):
5     x=x+1
6     print 'in the function ,the value is ',x
7 def test2(lists):
8     del lists[0]
9     print lists

 输出如下

{}
in the function ,the value is  5
original value is  4
[2, 3]

原文地址:https://www.cnblogs.com/tcstory/p/3317648.html