There is no such thing as a pass call in python, everything passed is a reference to an object, which can also be thought of as an address.
I. Mutable and immutable objects
Python allocates objects in the heap into two categories: mutable objects and immutable objects. Mutable objects are objects whose contents are mutable, while immutable objects are objects whose contents are immutable.
immutable (immutable): int, string (string), float, (numerical number), tuple (tuple)
mutable (mutable): word type (dictionary), list type (list)
Immutable type characteristics:
Look at the following example (Example 1)
i = 73 i += 2
As you can see from the above figure, the characteristics of immutable objects have not changed, all that has changed is that new objects have been created and the object references to the variables have been changed.
Look at an example (Example 2)
>>>x = 1 >>>y = 1 >>>z = 1 >>> x is y True >>>y is z True
As shown above, because the integer is immutable, x,y,z all point to a memory address in memory with a value of 1, i.e., x,y,z all point to the same address, and it's worth noting that, for shaping, only (-1,100) is currently supported.
To summarize, the advantages and disadvantages of immutable objects.
vantageYes, this reduces the amount of memory space occupied by duplicate values.
drawbacksIf I want to modify the value bound to this variable, as shown in Example 1, if there is no block of memory where the value exists, then a new block of memory must be opened and the new address must be bound to the variable name. Instead of modifying the value of the block of memory to which the variable was originally pointed, this will bring about a certain reduction in execution efficiency.
See below for an example of a mutable object (Example 3)
m=[5,9] m+=[6]
II. function parameters:
Python function parameters For mutable objects, changes to parameters within the function affect the original object; for immutable objects, changes to parameters within the function do not affect the original parameters. The reason:
1, variable object, the parameter changes the variable object, its content can be modified.
2, immutable object, the change is the function within the variable pointing to the object.
For example, there are two lists a and b
If a=b, a and b have the same address; if you just want to copy, then you have to use a=b[:]
def mutable(b = []): # The function uses the default variable (0) return b >>>mutable() [0] >>>mutable() [0,0] >>>mutable() [0,0,0]
Here three times in a row with the default value, run the function 3 this, each time the result is different, according to our want to think, three times the result, should be the same, are [0], but...
So what's the reason? As I said earlier, everything is an object, and the function mutable is also an object, use dir() to see the properties of the function:
dir(mutable) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
Above we ran the function mutable three times, and if you use mutable.__defaults__ to look at the default parameter changes to the function object, you'll see the problem.
>>>mutable.__defaults__ ([],) >>>mutable() [0] >>>mutable.__defaults__ ([0],) >>>mutable() [0,0] >>>mutable.__defaults__ ([0,0],)
On closer inspection, the value corresponding to 'x' in the internal attribute dict of the class object changes with each creation of the object. That is, the initial value of the variable x referring to memory is different each time the class object is created, which is ultimately due to the mutability of the list. Each time an object is created, because of the mutability of the list, the value corresponding to the x key, in the dict attribute of the function object b, is changed, rather than recreated, hence the above result.
Synthesis:Beginners who don't fully understand python's variables and types and the way arguments are passed, or how everything works with interpreted objects, will be prone to the errors above.
Above this python variable assignment method (mutable and immutable) is all I have to share with you, I hope it can give you a reference, and I hope you support me more.