Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the memory allocation mechanism of python?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the memory allocation mechanism of python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the memory allocation mechanism of python".

Start

As an example, let's create four variables and assign values to them:

Variable1 = 1variable2 = "abc" variable3 = (1Magne2) variable4 = ['axiaojie 1] # print their idsprint (' Variable1:', id (variable1)) print ('Variable2:', id (variable2)) print ('Variable3:', id (variable3)) print ('Variable4:', id (variable4))

The print result is as follows:

Variable 1RU 1747938368

Variable 2RU 152386423976

Variable 3RU 152382712136

Variable 4RU 152382633160

Each variable is assigned a new memory address (expressed as an integer). The first assumption is that every time we use "=" to assign a value to a variable, Python creates a new memory address to store the variable. Is this 100% correct? Of course not!

I will create two new variables (5 and 6) and assign values to them using the values of existing variables.

Variable5 = variable1variable6 = variable4print ('Variable1:', id (variable1)) print ('Variable4:', id (variable4)) print ('Variable5:', id (variable5)) print ('Variable6:', id (variable6))

Python print result:

Variable 1RU 1747938368

Variable 4RU 819035469000

Variable 5RU 1747938368

Variable 6RU 819035469000

Did you notice that Python didn't create new memory addresses for these two variables? This time, it simply points both new variables to the same storage location of the existing variables.

Now let's set a new value for variable 1. Note: integers are immutable data types.

Print ('Variable1:', id (variable1)) variable1 = 2print ('Variable1:', id (variable1))

This will print:

Variable1: 1747938368

Variable1: 1747938400

This means that whenever we use = and assign a new value to an existing variable, a new memory address is created internally to store the variable. Let's see if it works!

What happens when the value is a variable data type? Variable6 is a list. Let's append a value at the end of the list and print its memory address:

Print ('Variable6:',id (variable6)) variable6.append (' new') print ('Variable6:',id (variable6))

Note that the memory address of the variable remains the same because it is a mutable data type, and we have only updated its elements.

Variable6:678181106888

Variable6:678181106888

Let's create a function and pass a variable to it. What happens if we set the value of a variable inside the function? Let's evaluate it.

Def update_variable (variable_to_update): print (id (variable_to_update)) update_variable (variable6) print ('Variable6:', id (variable6))

Notice that the ID of variable_to_update points to the ID of variable 6.

This means that if we update variable_to_update in the function and variable_to_update is a variable data type, then the value of variable6 will be updated. Let's look at a specific example:

Variable6 = ['new'] print (' Variable6:', variable6) def update_variable (variable_to_update): variable_to_update.append ('inside') update_variable (variable6) print (' Variable6:', variable6)

This will print:

Variable6: ['new']

Variable6: ['new','inside']

It shows us how to update a variable in a function. You can see that both the function class and the variable outside the function have the same ID.

If we assign a new value (rather than an update) to a variable within a function, whether it is an immutable or mutable data type, once we exit the function, the change will be lost:

Print ('Variable6:', variable6) def update_variable (variable_to_update): print (id (variable_to_update)) variable_to_update = ['inside'] update_variable (variable6) print (' Variable6:', variable6)

Variable6: ['new']

344115201992

Variable6: ['new']

Here's an interesting scenario: Python doesn't always create a new memory address for all new variables.

Finally, what if we assign a string value to two different variables, such as "a"? Will it create two memory addresses?

Variable_nine = "a" variable_ten = "a" print ('Variable9:',id (variable_nine)) print (' Variable10:',id (variable_ten))

Note that these two variables have the same memory location:

Variable9:792473698064

Variable10:792473698064

What if we create two different variables and assign them a long string value:

Variable_nine = "a" * 21variable_ten = "a" * 21print ('Variable9:', id (variable_nine)) print ('Variable10:', id (variable_ten))

This time Python creates two different memory locations for two variables:

Variable9:541949933872

Variable10:541949933944

Why? This is because Python creates an internal value cache when it starts in order to provide faster results. Python assigns a small amount of memory addresses to a small number of integers (for example, between-5 and 256) and smaller string values. This is why all the short strings in our example have the same ID, while the ID of long strings is different.

= = vs Yes

Sometimes we want to check whether two objects are equal.

If we use = =, it will check whether the two parameters contain the same data

If we use is, then Python will check whether two objects refer to the same object, and the id of the two objects must be the same

Var1 = "a" * 30 var2 = "a" * 30 print ('var1:',id (var1)) # 318966315648 print (' var2:',id (var2)) # 168966317364 print ('=:', var1 = = var2) # return True print ('is:',var1 is var2) # return False above is the content of this article about "what is the memory allocation mechanism of python". I believe everyone has some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report