keys = {"status":True, 'name':'aaa'}
def fun1(keys):
print 'fun1'
print "keys:", keys
print "keys address", id(keys)
keys['status'] = False
def fun2(keys):
print 'fun2'
print "keys:", keys
print "keys address", id(keys)
fun1(keys)
fun2(keys)
#fun1
#keys: {'status': True, 'name': 'aaa'}
#keys address 49248184
#fun2
#keys: {'status': False, 'name': 'aaa'}
#keys address 49248184
从上面的结果看来,字典传入的是引用,在函数内部可以且值被改变了
下面在看看字符串
str1='hello world'
def fun1(str1):
str1="ni hao"
print "fun1 str1:", str1, "id:", id(str1)
print "str1:", str1, "id:", id(str1)
fun1(str1)
print "str1:", str1, "id:", id(str1)
#str1: hello world id: 39530000
#fun1 str1: ni hao id: 40521600
#str1: hello world id: 39530000