Python's data Model-objects, values, and types __python

Source: Internet
Author: User

In Python, all data is an object, or it is related to an object. (code can also be seen as an object).

Each object has an identifier, a type, and a value. Identity we can understand the memory address. When an object is established, the logo cannot be changed. " Is "to compare the identity of two objects, ID () This function takes object identification-an integer, in fact, is the memory address.

The type of the object can also be used to detect actions supported by the object. The possible values of this type of object are also defined. Type () This function returns the type of the object (the type itself is also an object).

The value of the object is variable and immutable, and there are two kinds of mutable objects and immutable objects. The English language is mutable and immutable, the object immutable (immutable) and value immutable (unchangeable value) are different. This comparison around, see the original text is relatively good understanding, the original text is (the value of an immutable container object that contains a reference to a Mutable object can change whe n the latter ' s value is changed; However the container is still considered immutable, because the collection of objects of it contains cannot. So, immutability isn't strictly the same as having a unchangeable value, it is more subtle. In other words, an container is an immutable object, assuming it contains a Mutable object reference, and the value of the package container changes when the value of the object it contains changes, but this time we say that the package container is still immutable, Because the collection of objects it contains is immutable. The variability of an object is determined by its type. For example, values, strings, and tuples are immutable, but dictionaries and lists are mutable.

Python, like Java, has a garbage collection mechanism. For Python garbage collection, we need to be aware of the following. There is a detailed control of recycling in the Python Library Reference. When you use the tracing and debugging tools, some objects that should have been recycled may not be reclaimed, and the This is also the case in the Try...except statement.

In addition, for some objects that contain "external" resources, such as files and Windows, it is best to use "try...finally" to display the release of these resources. This method is generally close ().

Let's take a look at the container (containers), which contains references to other objects. For example, tuples, lists, and dictionaries. In most cases, when we say the value of a container it refers to itself (the reference to the object is also part of the value), Rather than the contained object. When we discuss the variability of a container object, we simply say the identity of the contained object. Therefore, if an immutable object (such as a tuple) contains a Mutable object, the value of the Mutable object is changed as long as the value of the variable is changed.

For the type of object, it has a great effect on the behavior of the object, it even has an effect on the identity of the object. For Mutable objects (immutable), the operator that computes the new value may actually return a reference to an existing object that has the same type and value, for a mutable object (mutable), This is not allowed. For example, "A=1;b=1", A and B may point to an object with a value of 1.  (Or maybe not, this relies on a specific implementation), but for "c=[];d =[]", you can be sure that C and D save two different, separate, newly created empty lists. (Note that for c=d=[] this notation, C and D save the same object).

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.