SoFunction
Updated on 2024-11-16

Python parameter passing implementation process and principles of details

Before analyzing how parameter passing in python works, we need to understand, the basics of python variables and assignments, which will help us understand parameter passing better.

Python variables and assignments

numerical value

Start with a few lines of code

In [1]: a = 1
In [2]: b = a
In [3]: a = a + 1

First we assign 1 to a, which means that a points to the object 1, everything is an object in python. In python, everything is an object. Next, we assign b=a, which means that b also points to the object 1, and in python, an object can be pointed to by more than one reference. The last execution of a=a+1, here you need to pay attention to one point, python data types such as int, str and other immutable types, the execution of a=a+1 this operation, not to a point to the object of the value increased by 1, but to generate a new object 2, and let a point to 2 this object, the original object still exists in memory. Then here the words will still be pointed to, let's look at the value of a and b respectively:

In [4]: a
Out[4]: 2

In [5]: b
Out[5]: 1

As you can see from this example, a and b here start out as just two variables pointing to the same object, or you can think of them as two names for the same object. Simply assigning b = a doesn't recreate the new object, it just makes the same object pointed to or referenced by more than one variable. Also, pointing to the same object doesn't mean that the two variables are bound together. If you reassign a value to one of the variables, it doesn't affect the value of the other variable.

listings

There's another example of a list, so take a look at it again:

In [6]: l1 = [3,4,5,6]
In [7]: l2 = l1
In [10]: (7)
In [11]: l1
Out[11]: [3, 4, 5, 6, 7]
In [12]: l2
Out[12]: [3, 4, 5, 6, 7]

In the code, we let both variables l1 and l2 point to the object [3,4,5,6], we know that list is a mutable data structure, so the append operation will not generate a new object, but just add an element at the end, which becomes [3, 4, 5, 6, 7], since l1 and l2 point to the list at the same time, the changes in the list will be reflected in the list of l1 and l2 variables. Since l1 and l2 point to the list at the same time, the changes in the list will be reflected in both l1 and l2, and the values of l1 and l2 will be changed to [3, 4, 5, 6, 7] at the same time.

Object Deletion

Variables can be deleted in python, but objects can't be deleted.

In [22]: a = [1,4,5]

In [23]: del a

The del statement removes the variable a, and you can't access [1,4,5] through a. But the object still exists in existence, and python's garbage collection mechanism recycles it when it finds a reference to zero.

summarize

  • The assignment of a variable simply means that the variable points to an object, it does not mean that the object is copied to the variable; an object, on the other hand, can be pointed to by more than one variable
  • Changes to mutable objects (lists, dictionaries, collections, etc.) affect all variables pointing to that object
  • For immutable objects (strings, integers, tuples, etc.), the values of all variables pointing to the object are always the same and do not change. But updating the value of an immutable object by some operation (+=, etc.) will return a new object
  • Variables can be deleted, but not objects.

How python functions are parameterized

Python's parameter passing is assignment passing or reference passing. Everything is an object in python, so parameter passing just makes the new variable point to the same object as the original variable, so let's look at an example:

In [28]: def func(b):
  ...:   b = 2

In [29]: a = 1

In [30]: func(a)

In [31]: a
Out[31]: 1

The passing of the parameter here makes the variables a and b point to the object 1 at the same time. But when we get to b = 2, the system recreates a new object with a value of 2 and makes b point to it; a still points to the 1 object. So the value of a remains unchanged at 1.

How do you change the value of a?

We can return b in the function

def func(b):
  b = 2
  return b
a = 1
a = func(a)
a
2

In the example above ours is of type int, below we look at an example of a list:

def func(l2):
  (77)

l1 = [12,3,6]
func(l1)
l1
[12,3,6,77]

Here, l1 and l2 first point to the list of values [1, 2, 3] at the same time. However, since the list is mutable, when the append() function is executed and a new element 4 is added to the end of the list, the values of the variables l1 and l2 change.

So let's look at the following example, what is the result?

def func(l2):
  l2 = l2 + [4]

l1 = [12,3,6]
func(l1)
l1
[12,3,6]

As you can see, l1 has not changed because the operation l2 + [4] means that a new list is created with element 4 added to the end and l2 points to this new object, while l1 still points to the original object.

summarize

Today, we discussed the basics of Python's variables and their assignment, and explained how arguments are passed in Python. Unlike other languages, parameter passing in Python is neither value nor reference passing, but rather assignment passing, or what's called object reference passing. It's important to note that an assignment, or object reference, does not point to a specific memory address, but to a specific object.

This is the whole content of this article.