官术网_书友最值得收藏!

Argument passing is reference binding

So we've seen that it's quite possible to modify the objects through function argument references, but also that it's possible to rebind the argument references to new values. If you want to change the contents of a list parameter and have the changes seen outside the function, you could modify the contents of the list like this:

>>> def replace_contents(g):
... g[0] = 17
... g[1] = 28
... g[2] = 45
... print("g =", g)
...
>>> f
[14, 23, 37]
>>> replace_contents(f)
g = [17, 28, 45]

And indeed, if you check the contents of f you'll see that they have been modified:

>>> f
[17, 28, 45]

Function arguments are transferred by what is called "pass by object reference". This means that the value of the reference is copied into the function argument, not the value of the referred to object; no objects are copied.

主站蜘蛛池模板: 遂平县| 乌鲁木齐县| 寿光市| 定州市| 股票| 吴桥县| 宁德市| 永寿县| 龙南县| 延庆县| 孟州市| 宣威市| 呼和浩特市| 浙江省| 黑河市| 邢台县| 孟津县| 江西省| 苗栗市| 红安县| 公主岭市| 西贡区| 富裕县| 鹤山市| 娄烦县| 鄂伦春自治旗| 曲松县| 土默特右旗| 辉县市| 柳林县| 高邮市| 岢岚县| 万安县| 阳新县| 江安县| 镇平县| 宁津县| 登封市| 德化县| 诏安县| 阳曲县|