Variables and objects in Python

Source: Internet
Author: User
Tags garbage collection keyword list

I. What is a variable

Variables are common equations in previously learned mathematics, x = 3 (x is a variable, 3 is a variable value), in programming, the variable can be not only mathematical, but also arbitrary data type

two. Naming rules for variables

The variable name must be a combination of English case, number and _, cannot start with a number, cannot be a keyword

A            ? _name        ? Atest1       ? 1a           ?

So, how do you see the keywords in python?

#Import ModuleImportkeyword#Print a keyword listPrint(Keyword.kwlist)
Results: ['False','None','True',' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','finally',' for',' from','Global','if','Import','inch',' is','Lambda','nonlocal',' not','or','Pass','Raise','return','Try',' while',' with','yield']
three. Variables, objects, and references

In Python, everything is an object, everything is a reference to an object

To give the simplest example:

A = 3

, Python performs three steps to complete an assignment of a = 3:

1. Create also variable a

2. Create an object (allocate a piece of memory) to store the value 3

3. Connect the variable to the object, through the pointer, and the connection from the variable to the object called reference (variable Reference object)

four. The type belongs to the object, not the variable

In Python, we don't have to declare the type of the variable beforehand, and the variable type is automatically determined by the type of the object during the run.

To understand how the object type is used, consider the result of a variable that has been assigned multiple times:

A = 3                      # integer 'spam'                 # string a =  1.2.3                 # floating-point type

In the example above, a begins with an integer, then becomes a string and finally becomes a floating-point number. It seems that the type of a has changed three times.

In fact, in Python, a variable has no type and the type belongs to the object. As we said earlier, A = 3, the variable a refers to object 3, and the above three statements, in fact, variable a refers to different types of objects.

So what is the structure of the object?

As shown, each object contains two header information, one is a type identifier and the other is a reference counter. The type marker marks the type of the object, such as the integer object 3, which contains the value 3 and a type marker (strictly, it is a pointer to an object of int), which tells Python that this is an integer object. The identifier of the ' spam ' string object points to a string type. Because the objects record their type, the type of the variable is not necessary.

Five. Garbage collection of Objects

what happens to the previous reference when variable A is re-assigned?

A = 3'spam'

The answer is that when the variable A is re-assigned to the string ' spam ', its previous Reference object 3 is immediately recycled, and the object's space is automatically placed in a free-memory space pool, waiting for later objects to be used.

This is done inside Python: it allocates a counter inside each object that records the number of references currently pointing to the object, and once the calculator is set to 0 (that is, the object is not referenced by any variables), the object's memory space is automatically reclaimed. This mechanism for automatically reclaiming object space is called garbage collection, and the best thing about it is that you can reuse objects in your scripts without having to think about freeing up memory space, and Python will clean up the space that is no longer in use when the program is running.

Variables and objects in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.