From the perspective of memory management, an illustration of variable and parameter transfer in Python language

From the perspective of memory management, an illustration of the transfer of variables and parameters in Python language

Overview

From a memory management perspective, variable and parameter passing in Python has some characteristics:

☆ Variables are references to objects: In Python, variables are actually references to objects, not the objects themselves. When you assign a value to a variable, you actually point the variable to an object. This means that variables can point to different types of objects, and the pointed objects can be changed at any time in the program.

☆ Reference counting: Python uses reference counting to manage memory. Every object has a reference count that indicates how many variables refer to the object. When the reference count reaches 0, the object will be automatically recycled. When a variable no longer refers to an object, the reference count is decremented. When the reference count reaches 0, the object’s memory will be released.

☆ Mutability of objects: Objects in Python are divided into mutable objects and immutable objects. The values of mutable objects (such as lists and dictionaries) can be modified, while the values of immutable objects (such as integers, strings, and tuples) cannot be modified. This means that if a mutable object is modified, all variables that reference this object will be affected.

☆ Parameter passing method: In Python, the parameters of a function are passed by call by value (the value is always a reference to the object rather than the value of the object) – in fact, call by object reference ( call by object reference) is better. For immutable objects (such as integers, strings, and tuples), since their values cannot be changed, modifications to these objects within the function actually create a new object. Therefore, modifications inside the function will not affect the actual parameters outside the function. For mutable objects (such as lists and dictionaries), since their values can be changed, modifications to these objects within the function will directly change the value of the original object. Therefore, modifications inside the function will affect the actual parameters outside the function.

In Python, the id() function is used to obtain the unique identifier (i.e. memory address) of an object. Each object (in Python language, all data is called an object) has a unique identifier in memory, which can be obtained through the id() function.

For example, id(8.53), it will return the unique identifier (memory address) of 8.53. This identifier is an integer value that uniquely identifies the location of the object in memory, similar to 2200635092656. Note that this value may be different in different runtime environments because it Depends on the specific implementation of memory management and object allocation. Python’s memory management and object allocation model is automatic and is taken care of by Python’s memory manager.

Let’s look at the following situation first

Explanation: In Python, the id() function returns the memory address of the object, which is a unique identifier for each object.

First, when id(21.37) is called, Python creates a floating point object 21.37 and returns its memory address, which is 2491682696880. As shown below:

Then, a variable a is created and assigned the value 21.37. At this point, Pythoncreates a new floating point number object again 21.37 (because floating point numbers are immutable) and points a to this new object. Therefore, when id(a) is called, it returns the memory address of this new object, which is 2491682692176. As shown below:

Next, a new variable b is created and assigned the value a. In this case, Python does not create a new object, but makes b point to the object pointed to by a. Therefore, a and b actually point to the same object, so id(b) returns the same memory address 2491682692176. As shown below:

Finally, when b is printed, it displays the value of the object pointed to by b, which is 21.37.

Tip: The mechanism of variable assignment statement variable = expression in Python:

1. Calculate the value of the expression: Python will first calculate the value of the expression on the right side of the equal sign.

2. Create a new object: Python will create a new object for this value.

3. Variable binding: Python will bind the variable name to the newly created object, or the variable points to this object.

This is the basic mechanism of variable assignment in Python. It should be noted that for some specific types of objects – such as small integers (integers between -5 and 256), and specific short strings, such as shorter ones that only contain ASCII characters and do not contain spaces, numbers or special characters A literal string of characters. When the Python interpreter starts, it pre-creates and caches a range of integers and some commonly used short string objects. When you use these values in your code, Python does not create new objects, but directly references existing objects. This is the so-called “interning” mechanism. This mechanism can improve the operating efficiency of Python, because for these commonly used small integers and short strings, Python can avoid repeatedly creating and destroying objects, thereby saving memory and CPU resources.

Let’s look at the following situation:

Explanation: In Python, small integer objects (usually integers between -5 and 256) are pre-created and reused. This is to optimize memory usage and performance, since these small integers are used frequently in most Python programs. So when a variable a is created and assigned a value of 2, and then a variable b is created and assigned a value of 2, they actually point to the same memory object.

For large integers (usually those greater than 256), Python creates a new object for each new integer. So when you create a variable a and assign it a value of 1500, and then create a variable b and assign it a value of 1500, they actually point to two different memory objects. That’s why id(a) and id(b) return different values. See picture below:

Variables and assignments in Python language

In the Python language, all data are called objects.

Every integer, decimal, string, and dictionaries, tuples, lists, etc. that we will learn later, are all objects.

In order to facilitate the operation of data objects, programming languages also need to give the object a name, which is called a variable name, sometimes also referred to as a variable.

Variables in Python are the names of objects.

In Python, names are used to refer to objects. Names are introduced via name binding operations.

[See official documentation naming and binding

https://docs.python.org/zh-cn/3/reference/executionmodel.html#naming-and-binding

Tip: The term “name” is used more often in Python’s official documentation. But in actual coding and communication, people often use the term “variable” to describe names in Python. 】

Python is a dynamically typed language, so we do not need to declare the variable type in advance, we can directly assign values to the variables.

You can use an assignment statement to create a variable and make it reference a piece of data. When a variable represents a value in memory, it is also said to refer to that value.

The basic format of the assignment statement is:

variable = expression

The equal sign (=) is called the assignment operator. In this basic format, variable is the name of a variable and expression is a value, or code that produces a value. After the assignment statement is executed, the variable on the left side of the equal sign will reference the value on the right side of the equal sign.

Tip: You cannot use a variable before assigning a value to it. Performing some operation on a variable, such as printing it, before it is assigned a value will result in an error.

[Note: Variables in the Python language work differently than variables in most other programming languages. In most programming languages, a variable is a memory location that holds a value. In these programming languages, when a value is assigned to a variable, the value is saved in the memory location of the variable. In the Python language, a variable is a memory location that holds the address of another memory location. When you assign a value to a Python variable, the value is stored in a memory location separate from the variable. A variable holds the address of a memory location that holds a value. That’s why in Python, instead of saying a variable “holds” a value, we say a variable “references” a variable. In other words, a variable in Python is a name that represents a value stored in computer memory. 】

Variables are called “variables” because they can refer to different values during the execution of a program. Let’s take a look at the assignment and reassignment of variables in Python.

Creates a variable named dollars and assigns 2.75 to it

Dollars = 2.75

Assign a new value of 99.95 to the variable dollars. When you assign a new value of 99.95 to the variable dollars, although the old value of 2.75 is still stored in the computer’s memory, because there is no variable to reference it, this value can no longer be used. See picture below:

Note that when values in memory are no longer referenced by variables, the Python interpreter will automatically move them out of memory through a so-called garbage collection mechanism.

[In Python, variables store references to objects (values), and the id() function can obtain the address of the variable in memory. In Python, values can be placed at a certain location (address) in memory, and variables are used to reference them. When a new value is assigned to a variable, the original value will not be overwritten by the new value, the variable just references the new value. By the way, Python’s garbage collection mechanism will automatically clean up values that are no longer used, so you don’t have to worry about computer memory being filled with invalid values that are “discarded”. 】

It is important to remember that in the Python language, a variable is just a name that refers to a piece of data in memory, and can refer to any type of data item.

The x = 99 statement creates a variable named x and assigns it the int value 99.

When assigning a string ‘Take me to your leader’ to variable x, variable x no longer refers to an int type data, but refers to the string ‘Take me to your leader’.

Let’s look at this situation again:

a=10

b = a

In the above example, we declared a variable a with a value of 10, and then pointed b to a. This is the layout in memory. The variables a and b will point to the same object 10, instead of giving b Regenerate a new object.

After a = a + 10 is executed, because the integer is an immutable object, 10 will not be changed to 20, but a new object 20 will be generated, and then a will point to this new object. b still points to the old object 10.

Python allows you to assign values to multiple variables at the same time. For example:

a = b = c = 10

Taking the above example, create an integer object with a value of 10, assign values from back to front, and the three variables are assigned the same value.

You can also specify multiple variables for multiple objects. For example:

a, b, c = 10, 20, “hello”

Taking the above example, two integer objects 10 and 20 are assigned to variables a and b, and the string object “hello” is assigned to variable c.

Parameter passing in Python

To explain clearly the parameter passing of functions in Python, we need to start with what a function is in Python.

In Python, a function is a way of organizing code that encapsulates a piece of program code for easy reuse. Functions can receive input parameters and return one or more results. This encapsulation can make the code clearer and easier to read, and also facilitates code reuse. Python provides many built-in functions, such as print(), len(), etc., which can be used directly. Programmers can also customize functions. The custom icon is as follows:

Function definitions in Python use the def keyword, followed by the function name, brackets (), and colons. Function parameters can be placed in parentheses and separated by commas. The body of a function is defined in an indented block after the colon. The syntax for defining functions in Python is as follows:

def function name (parameter list):

function body

The function body consists of indented blocks of code. Python’s function body includes:

The function body includes the following parts:

Documentation string (docstring): Documentation string is optional and is used to describe the function’s function, parameters, return value and other information. It is located on the first or second line of the function definition, using triple quotes (”’or “””) brackets. The docstring can be accessed through thehelp() function or using the __doc__ attribute in an interactive environment.

Statements: The function body can contain any number of statements to perform specific tasks. These statements can be assignment statements, conditional statements, loop statements, etc., and are written according to the needs of the function.

Return expression: A function can return a value through the return statement. The return value is optional. If there is no return statement or there is no expression after return, the function will return None. When the function executes to the return statement, the function will end immediately and the return value will be passed to the place where the function was called.

The following introduces python parameter passing

According to the official Python documentation [https://docs.python.org/zh-cn/3/tutorial/controlflow.html#defining-functions] actual parameters are passed using call by value (where The value is always a reference to the object rather than the value of the object) – in fact, call by object reference is a better way of saying it. Specifically:

If the parameters passed in are of immutable types (such as numbers, strings, tuples), then modifying the parameter values in the function body will not affect the original variables. Because variables of immutable types are actually references to values, when you try to change the value of a variable, you are creating a new object. For example:

def change_number(num):
    num = 100

x = 10
change_number(x)
print(x) # Output: 10

In the above example, although the value of num has been changed inside the function, the value of the original variable x has not changed. See picture below:

If the parameter passed in is of a variable type (such as a list, dictionary), then modifying the value of the parameter in the function body will affect the original variable. Because a variable type variable stores an address, when you try to change the value of the variable, you are actually changing the content pointed to by the address. For example:

def change_list(lst):
    lst.append(100)

x = [1, 2, 3]
change_list(x)
print(x) #Output: [1, 2, 3, 100]

In the above example, the modification of the parameter lst inside the function affects the value of the original variable x. See picture below:

The official statement of Python’s parameter passing mechanism: call by object reference. This object is shared between the caller and the callee. For immutable objects, since they cannot really be modified, modifications are often achieved by generating a new object and then assigning a value.

The following is an excerpt from the official website document [https://docs.python.org/zh-cn/3/tutorial/controlflow.html#defining-functions]

When a function is called, the actual parameters (actual parameters) are introduced into the local symbol table of the called function; therefore, the actual parameters are passed using call by value (where the value is always a reference to the object and not the value of an object)[1].

[Note 1] In fact, calling by object reference is better because when a mutable object is passed, the caller can discover any changes made by the callee (elements inserted into the list ).

Also: How to write a function with output parameters (called by reference)? [See https://docs.python.org/zh-cn/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference]

Remember that arguments in Python are passed by assignment. Since assignment only creates a reference to the object, there are no aliases for the parameter names of the caller (caller) and the callee (callee), and there is essentially no call-by-reference method. You can get the desired effect in the following ways. (The following is an example, omitted)

To break it down, this means: if you try to reassign a new object to a parameter inside a function, this change will not affect the actual parameter outside the function. Because in Python, variable names and data are separate, the variable name is just a reference to the data, not the data itself. However, there are ways you can achieve something like pass-by-reference. For example, you can pass a mutable object (such as a list or dictionary) and then modify this object inside the function. Since a reference to the object is passed, these modifications are reflected outside the function. This is called “pass by sharing”.

Appendix,

Understanding of Python variables https://blog.csdn.net/cnds123/article/details/116768499
python function https://blog.csdn.net/cnds123/article/details/108179769

Introduction to parameter passing methods in various (C++, Java, JavaScript, Python) programming languages https://blog.csdn.net/cnds123/article/details/132981086