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 ',x10 lists=[1,2,3]11 b.test2(lists)

1 def testing(test):2     del test['a']3     print test4 def test1(x):5     x=x+16     print 'in the function ,the value is ',x7 def test2(lists):8     del lists[0]9     print lists

 输出如下

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

转载于:https://my.oschina.net/tcstory/blog/466237

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值