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

How to assign variables in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to assign variables in Python". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how to assign variables in Python".

Introduction:

The variables in Python are very smooth in use, and you can ignore the type and assign values at will, which improves the efficiency of the development. However, if you do not understand the mechanism, you will often make some small mistakes and make the development not so smooth. This article is to take you to understand the variables in Python from the perspective of language design and underlying principles.

Let's start with a simple example:

A = 3

Conceptually, Python takes three different steps to complete the request when we write axi3 in our code:

An object is created to represent a value of 3

If a has not been created, a variable a will be created.

Associate variable a with the new object 3, and call variable an a reference to object 3

The three keywords mentioned above, "object", "variable" and "reference" are the key to making variables work in Python, which we will discuss in more detail in the next section.

1 variables, objects, references

Let's start with the concept:

Objects are allocated pieces of memory, and there is enough space to represent the values they represent.

A variable is an element of a system table that has space for connections to objects.

A pointer from a variable to an object that is automatically formed when referenced.

Following the three steps of axi3 above, let's add a sentence of code:

A = 3b = a

The following is a chart showing the results of two sentences:

The action raised by baccouna is also pointed to 3, establishing a reference from the variable b to object 3, which implements the assignment operation of python. This section leads to the secret of the assignment operation in Python. Let's take a look at why we don't specify the type of variable when assigning a variable in Python. In fact, an important definition has been found from the above concept:

Types belong to objects, not variables

To understand how object types are used, let's look at the results of multiple assignments to a variable:

A = 3a = 'wali'a = 3.1415926

On the face of it, a starts as an integer, then becomes a string, and finally becomes a floating point, which is incomprehensible to people who have studied C, but for python, it is executable. It looks like the type of an is changing continuously, but in fact, after we understand the concepts of variables, references, objects, and "types belong to objects, not variables," we will find that the following things actually happen:

In the execution, the integer type object 3, the string object "wali" and the floating point object 3.14 are created respectively. The variable a does not own these types, but simply points to three objects by reference.

On further investigation, we will find that from the perspective of Python language implementation, each object contains a header information that identifies the type of the object.

In addition, there is a concept of "reference counter". Let's take a look at the original code:

A = 3b = a

Perhaps the smart reader has quietly calculated that the value of the reference counter for object 3 is 2, which is the reference of variable an and variable b to object 3, respectively. Yes, the definition of the reference counter is so clear that it represents the number of references used to point to the same object. Through the assignment operation between variables, the reference count of objects is calculated automatically.

So, we ask what's the use of reference counters, and why bother to calculate how many variables refer to the same variable? at this point, we come up with a new concept: object garbage collection.

2 garbage collection mechanism of object

There is a piece of code:

A = 3a = 'wali'a = 3.1415926

We're going to think further, what happened to object 3 when I changed a from pointing to integer object 3 to pointing to string object 'wali'? Don't you keep it in memory all the time, and if the object is very large, it takes up a lot of memory? in fact, the Python designers have been very thoughtful for us:

In Python, every time a variable name is assigned a new object, the previous object space is reclaimed (provided that the object is not referenced by another variable name or object). This technique of automatically reclaiming object space is called garbage collection.

How to determine when to recycle, we have to use a very important concept mentioned in the previous section, the object reference counter, when the counter value is 0 to identify no variable or object reference, automatically reclaim the object space. At this point, we understand the important role of object reference counters, and understand that in addition to the code we see, Python is quietly doing a lot of automated things for us.

What is the difference between the objects pointed to by the 3 variables? # example 1a = 3b = aa = 5

Let's go back to the above example, if a changes, will b change with it? Theoretically, pointing to the same object will change, but the answer here is no, because object 3 is a number and immutable object, so you can only recreate a new object 5, and then a points to object 5. But if the object a points to is a mutable object, such as a list, b will change as we think, as shown in the following example:

# example 2a = [1Jing 2jue 3] b = aa [0] = 3L = an is b > TrueM = a = = b > > True

So look at the following example, what's the difference between example 2 and example 3?

# example 3a = [1True 2pr 3] b = [1m 2je 3] L = an is b > FalseM = a = = b > True

In python, there are two methods to detect whether variables are equal, is and = =, where = = is to determine whether the value of the object pointed to by the variable is equal, and is is to judge the identity of the object. If the two variables point to the same object exactly, the is operator will return True, which can also be understood as the is operator, which compares whether the pointer of the reference is the same. In example 2, the variable an and the variable b point to the same object. So both L and M are True, but in example 3, the variables an and b point to different objects, so there is a difference in the values of L and M below example 3, but if the following example shows a different result:

# example 4a = 3b = 3L = an is b > TrueM = a = = b > True#example 5c = [1Mei 2Mei 3] K = c [2] is a > True

Why is that? Because 3 is an immutable object, only one copy is retained in order to save memory consumption. No matter how many references point to object 3, there is only one copy of object 3, which is well proved by example 5.

Reference relationship between variables and objects in examples 4 and 5

Extracurricular knowledge:

(1) variable types, values can be changed: mainly include list list, dict dictionary; immutable types, values cannot be changed: mainly include: numerical types int, long, bool, float, string str, tuple tuple

In example 3, the objects pointed to by variables an and b are mutable, and the addresses of an and b are different, but the elements in an and b point to the same object, as shown in the following figure

These are all the contents of the article "how variables in Python are assigned". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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