Python method of parameter passing

Before analyzing how the parameter transfer in python is carried out, we need to understand the basic principles of python variables and assignment, which will help us better understand parameter transfer.

Python variables and assignment

Start with a few lines of code

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

We first assign 1 to a, that is, a points to the object 1, and everything is an object in python. Then b=a, which means that b also points to the object 1. In python, an object can be pointed to by multiple references. The last executed a=a+1, we need to pay attention to one point here. In python's data types such as int, str and other immutable types, performing a=a+1 does not increase the value of the object pointed to by a by 1. Instead, create a new object 2, and let a point to the object 2. The original object still exists in memory. Then the words here will still be pointed, let's take a look at the values of a and b respectively:

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

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

Through this example, you can see that here a and b are just two variables pointing to the same object at the beginning, or you can also imagine them as two names of the same object. The simple assignment of b = a does not mean that a new object is re-created, but that the same object is pointed to or referenced by multiple variables. At the same time, pointing to the same object does not mean that the two variables are bound together. If you reassign one of the variables, it will not affect the value of the other variables.

There is also an example of a list, let's take a look again:

In [6]: l1 =[3,4,5,6]
In [7]: l2 = l1
In [10]: l1.append(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 the two variables l1 and l2 point to the object [3,4,5,6]. We know that the list is a variable data structure, so the append operation does not generate new objects. It just adds an element at the end and becomes [3, 4, 5, 6, 7]. Since l1 and l2 point to this list at the same time, the change of the list will be reflected on both variables l1 and l2 at the same time, then , The values of l1 and l2 become [3, 4, 5, 6, 7] at the same time.

Variables in python can be deleted, but objects cannot be deleted

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

When the del statement deletes the variable a, you cannot access [1,4,5] through a, but this object still exists in existence, and Python's garbage collection mechanism finds that the reference is 0, it will be recycled.

  1. The assignment of a variable only means that the variable points to an object, but does not mean that the object is copied to the variable; and an object can be pointed to by multiple variables
  2. Changes to mutable objects (lists, dictionaries, collections, etc.) will affect all variables that point to the object
  3. For immutable objects (strings, integers, tuples, etc.), the value of all variables pointing to the object is always the same and will not change. However, when the value of an immutable object is updated through certain operations (+= etc.), a new object will be returned

Variables can be deleted, but objects cannot be deleted

How does python function pass parameters

Python's parameter transfer is assignment transfer or reference transfer. Everything in python is an object, so when passing parameters, just let the new variable and the original variable point to the same object. 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 parameter passing here makes the variables a and b point to the object 1 at the same time. But when we execute to b = 2, the system will recreate a new object with a value of 2, and let b point to it; and a still points to the object 1. Therefore, the value of a remains unchanged and remains 1.

How to change the value of a?

We can return b in the function

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

In the above example, we are of type int, let's take a look at the list example:

def func(l2):
 l2.append(77)

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

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

Then everyone 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 of l2 + [4] means that a new list is created with element 4 added at the end, and l2 points to this new object, and l1 points to the original object.

to sum up

Today, we discussed the basic principles of Python variables and their assignments, and explained how parameters are passed in Python. Unlike other languages, parameter passing in Python is neither by value nor by reference, but by assignment, or called object by reference. It should be noted that the assignment or object reference transfer here does not point to a specific memory address, but to a specific object.

The above is the detailed content of python's parameter passing method. For more information on how python performs parameter passing, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

Python method of parameter passing
Summary of logarithm method in Python
End the method of running python
7 features of Python3.9
Method of installing django module in python
The specific method of python instantiation object
Example method of reading sql from python
Detailed explanation of -u parameter of python command
The specific method of python import library
Method analysis of Python calling C language program
Detailed explanation of the principle of Python super() method
Python magic method topic
Python object-oriented magic method
Python3.7 debugging example method
Basic knowledge of Python (1)
Python error handling method
09. Common modules of Python3
Detailed explanation of the principle of Python function parameter classification
Python defines a function method
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Python TCP packet injection method
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Python tracking except information method
Analysis of JS of Python crawler
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Python implements gradient descent method
Python version of OpenCV installation
9 feature engineering techniques of Python
Python right alignment example method
matplotlib of python drawing module
Consolidate the foundation of Python (3)
Collection of Python Common Modules
Python arithmetic sequence calculation method
The usage of wheel in python
Method steps to increase python font
Use of Pandas in Python development
Detailed implementation of Python plug-in mechanism
Python and js interactive call method
Detailed explanation of python sequence types
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Magic methods and uses of Python
In-depth understanding of Python variable scope
Python preliminary implementation of word2vec operation
Python handles the 4 wheels of Chinese
Python calculation of information entropy example
Python function definition and parameter explanation
Implementation of python selenium operation cookie
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Scrapy simulation login of Python crawler
Analysis of Python conditional control statements