Arguments pass reference or value ? Mutability

Mutability asks the question of whether an object enables the modification of its value. All Python objects have the following three attributes: type, id, and value.Type and value should hopefully be obvious; the “id” is simply an identification number that uniquely identifies an object from all other objects running in the interpreter.
All three attributes are almost always read-only, meaning they cannot be changed during the lifetime of an object.The only possible exception is the value: If the value can be changed, then it is a mutable object; otherwise it is immutable. Simple or “scalar” types, including integers and other numeric types, are immutable as are string types like str and unicode, and finally tuples. Just about everything else—lists, dicts, classes, class instances, and so forth—is mutable.
One area of caution with mutable objects is in calling their methods. If the method you call modifies a mutable object in any way, then it typically does so “in place,” meaning the data structure is modified directly instead of returning a modified copy of the object (in which case the function returns None instead).
mylist = [1, 'a', ['foo', 'bar']]
mylist2 = list(mylist)
mylist2[0] = 2
mylist[2][0] = 'biz'

then the list 'mylist2' is a copy of the list mylist, but you must know that the list() is a function too. Then the inner list of the list mylist is copyed by reference to the new list mylist2.

原文地址:https://www.cnblogs.com/henyihanwobushi/p/2676962.html