Python引用传值总结

Python函数的参数传值使用的是引用传值,也就是说传的是参数的内存地址值,所以在函数中改变参数的值,函数外也会改变。python

这里须要注意的是若是传的参数类型是不可改变的,如String类型、元组类型,函数内如需改变参数的值,则至关于从新新建了一个对象。app

# 添加了一个string类型的元素添加到末尾

def ChangeList(lis):
    lis.append('hello i am the addone')
    print lis
    return

lis = [1, 2, 3]
ChangeList(lis)
print lis

获得的结果是:函数

[1,2,3, 'hello i am the addone']this

[1,2, 3,'hello i am the addone']对象

def ChangeString(string):
    string = 'i changed as this'
    print string
    return

string = 'hello world'
ChangeString(string)
print string

String是不可改变的类型,获得的结果是:blog

i changed as this内存

hello worldstring

相关文章
相关标签/搜索