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

The reference to the python variable and how it is stored at the underlying level

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the reference of python variables and the principle of storage at the bottom. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Python variables, to put it simply, there are six categories: numeric, Boolean, string type, list, tuple, dictionary and so on. So how different variable types are stored at the bottom is related to the reference of variables and whether they can correctly master the relevant operations of variables.

What are the values of v1 and v2 below? Why?

V1 = 3v2=v1print ("v2:", v2) v1 + = 2print ("v1:", v1) print ("v2:", v2)

# what is the value of L2 below? Why?

L1 = [1 l1print (L2) l1.append (4) print (L2) 1. The underlying principle of references to variables

V1 = 3v2=v1 print ("v2:", v2) v1 + = 2 print ("v1:", v1) print ("v2:", v2) # v1 has changed, why does the value of v2 not change with it?

The implementation results are as follows:

V1: 5

V2: 3

''

L1 = [1jin2jin3] L2 = l1print (L2) l1.append (4) print (L2) # L1 has changed, but why has the value of L2 changed as well.

The implementation results are as follows:

[1, 2, 3]

[1, 2, 3, 4]

''

Analysis of key points:

Immutable objects and immutable objects: immutable objects include int,float,string,tuple, etc. Mutable objects include list,dict, instances of custom classes, and so on.

The variables copied by = in python pass both the value of the variable and the address of the variable in memory. That is, the copied variable is not only the same memory address, but also the value of the variable. But it should be noted that for variables of immutable types (such as int), if you want to change the value of the variable, a new value will be created, a new memory address will be assigned to the new value, and the variable will be pointed to the memory address of the new value, while the old value will wait for garbage collection if it is not referenced. If it is a variable type variable. If you change the value of a variable, you can modify the value of the variable directly, and the reference address of the variable will not change.

Immutable type: the variable assigns a value of axi5 and then assigns a value of axi10. Here, a new int value object 10 is generated, and then a points to it, while 5 is discarded, not changing the value of a, which is equivalent to generating a newly.

Variable type: variable assignment la= [1 Magne2 Magne3 4] and then assign la [2] = 5 is to change the value of the third element of list la, the la itself is not moved, but part of its internal value has been modified.

The essence is that once an immutable object is created, the system will allocate fixed memory to him according to his size, so it is not allowed to modify, only to modify the value can only apply for new memory and address. On the other hand, the memory size of a variable object can be automatically expanded with the change of the value.

Code analysis demo:

Because when a variable is created in memory, the system will assign him an address, and then use the address to find or reference his value. All variables in Python are actually pointers to objects in memory, references to values.

Code demonstration 1:

V1 = 3v2=v1print ("address of v1 in memory:% id v2 in memory:% d"% (id (v1), id (v2)) v1 + = 2print ("address of v1 in memory:% dgraine v2 in memory address:% d"% (id (v1), id (v2)) print ("v1:", v1) print ("v2:", v2) = address of v1 in memory: 1747378992 address v2 in memory: address of 1747378992v1 in memory: 1747379024 V2 is in memory address: 1747378992v1: 5v2: 3 minutes'

Because: v1JEI v2 are int, immutable objects, so once you modify their values, package deduction, multiplication, division, assignment and other operations, the system will re-create one for them in memory.

Then bind the variable (by address reference) to the value, so after v1 + = 2, his address in memory changes. And v2 still refers to the previous address, so the value of v2

There are no updates.

''

Code demo 2:

L1 = [1id (L1), id (L2)) l1.append (4) print (L2) print ("address of L1 in memory:% d" (id (L1) = address of L1 in memory:% d "% (id (L1) L2 in memory address: 37345880 [1,2,3] L1 in memory address: 37345880 L2 in memory address: 37345880 [1,2,3,4] L1 in memory address: 37345880

For mutable objects such as lists, when you change their values, the system automatically expands their memory at the original address, so there is no need to change the address.

Code demonstration 3: the same is true of immutable variable references such as strings.

Str1 = 'hahh'str2 = str1str1 + = "123" print (str1,str2) hahh223 hahh2. Classification of variables

Mutable type and immutable type

Variable type, the value can be changed:

List list

Dictionary dict

Immutable type, the value cannot be changed:

Numeric types int, long, bool, float

String str

Tuple tuple

These are the references to python variables and how they are stored at the underlying level. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, 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