- The Python Apprentice
- Robert Smallshire Austin Bingham
- 153字
- 2021-07-02 22:17:05
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.
推薦閱讀
- Java語言程序設計
- What's New in TensorFlow 2.0
- Python數據可視化:基于Bokeh的可視化繪圖
- Mastering Spring MVC 4
- C語言程序設計學習指導與習題解答
- Mastering Apache Maven 3
- Oracle 18c 必須掌握的新特性:管理與實戰
- Python之光:Python編程入門與實戰
- Mastering Xamarin.Forms(Second Edition)
- RealSenseTM互動開發實戰
- Visual C++從入門到精通(第2版)
- 3ds Max 2018從入門到精通
- 用Python動手學統計學
- Apache Solr for Indexing Data
- Python滲透測試編程技術:方法與實踐(第2版)