[python] pass by reference or value 參數是傳參考還是傳值?

  • 0
Referencing: Python: How do I pass a variable by referencePassing values in Python
其實重點是在 data type. 
到底在寫 function 的時候,傳的參數 (argument, parameter) 是 reference or value?
全部都是passes references-to-objects by value (like Java), 只是 mutable data type would act like pass by reference


Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?
It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

沒有留言 :

張貼留言