The difference between assignment in python and c language

爱喝马黛茶的安东尼
Release: 2019-06-24 13:09:29
Original
3389 people have browsed it

The difference between assignment in python and c language

What is the difference between assignment in python and c language? Let’s first take a look at what a simple Python code looks like in memory:

b = 3
b = b + 5
Copy after login

It The operation diagram in memory is as follows:

The difference between assignment in python and c language

However, from the literal meaning of the code, "Assign 3 to b, add 5 to b and then assign Give it to b."

That is, the code looks like this:

b ← 3
b ← b + 5
Copy after login

So the following operation diagram in memory may be more in line with our intuition:

The difference between assignment in python and c language

That is, the value of b 5 is written back to b. This is what a typical C program looks like. Allocate a memory unit of type int for variable b, and then store the integer 3 in the memory unit. b represents the block of memory space and will no longer move. The value of b can be updated, but the address of b in the memory will no longer change. So we say b = b 5, which is equivalent to b ← b 5. After adding 5 to the value of b, it is still placed in b. Variable b is tightly bound to the memory space where it resides.

Related recommendations: "Python Video Tutorial"

Looking at the memory diagram in Python above, b 5 gets a new value, and then makes b point to this new value. In other words, what it does is this:

b → 3
b → b + 5
Copy after login

Make b point to 3, and then make b point to the new value of b 5.

The C program updates the value stored in the memory unit, while Python updates the pointer to the variable.

Variables in C programs store a value, while variables in Python point to a value.

If the C program indirectly manipulates data by manipulating memory addresses (each variable corresponds to a fixed memory address, so manipulating variables means manipulating memory addresses), and the data is in a passive position, then Python directly manipulates it. Data, data is in an active position, and variables only exist as a reference relationship and no longer have a storage function.

In Python, each data occupies a memory space. For example, the new data b 5 also occupies a brand new memory space.

This operation of Python makes data the subject, and data interacts directly with data.

Data is called an object in Python.

This sentence is not too rigorous. But it works in this simple example.

An integer 3 is an int type object, a 'hello' is a string object, and a [1, 2, 3] is a list object.

Python regards all data as "objects". It allocates a memory space for each object. After an object is created, its id no longer changes.

id is the abbreviation of identity. It means "identity; identification".

In Python, you can use id() to obtain the id of an object, which can be regarded as the address of the object in memory.

After an object is created, it cannot be destroyed directly. Therefore, in the previous example, variable b first points to object 3, and then continues to execute b 5. b 5 produces a new object 8. Since object 3 cannot be destroyed, let b point to the new object 8 instead of Use object 8 to overwrite object 3. After the code execution is completed, there is still object 3 and object 8 in the memory, and the variable b points to object 8.

If there is no variable pointing to object 3 (that is, it cannot be referenced), Python will use the garbage collection algorithm to decide whether to recycle it (this is automatic and does not require programmer to worry about it).

An old object cannot be overwritten. New data generated due to interaction with the old object will be placed in the new object. In other words, each object is an independent individual, and each object has its own "sovereignty". Therefore, the interaction of two objects can produce a new object without affecting the original object. In large programs, the interactions between objects are complex, and this independence makes these interactions safe.

The C program assigns a fixed memory address to each variable, which ensures the independence between C variables.

C language is the interaction between variables (that is, memory addresses), and Python is the interaction between objects (data). These are two different ways of interacting.

The above is the detailed content of The difference between assignment in python and c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!