The basis of Python-object

Source: Internet
Author: User

对象(Objects)Is the abstraction of data in Python, and all data in Python can be represented by the relationship between an object or an object. Each object has a 标识符(identity)、类型(type)、值(value) .

    1. Identifier. Once an object is created, its identifier will not change, and the identifier can be treated as an object's address in memory. The is operation can be used to compare the identifiers of two objects, and the function ID () is used to return an object identifier (the address in Python that returns an object in memory).
    2. Type. The type of the object is also immutable, and the type of the object determines the operations that the object supports, and also determines the possible values for that object. The type () function returns the types of an object.
    3. Value. The values of some objects can be changed, we call it mutable objects , dictionaries and lists are mutable objects; Values immutable objects we call it immutable objects , numbers, strings, tuples are non-mutable objects.

In Python, there is no so-called call-to-value, everything is a reference to an object, and it can be thought of as a pass-through address.

Mutable objects and immutable objects

Python allocates objects in the heap into two categories: mutable objects and immutable objects. A mutable object refers to the variable content of an object, while an immutable object refers to the content of an object that is immutable.

Immutable (immutable) objects: int, string, float, numeric (number), tuple (tuple).

Variable (mutable) objects: Word-typical (dictionary), List-type (lists).

I. Immutable objects

Because variables in Python hold object references, object references to variables are mutable for non-mutable objects, although the object itself is immutable.

i = 73  

As shown in the illustration above, object 73 and Object 75 do not change, only the creation of a new object, changed the object of the variable reference. Take a look at the following code to show this:

#因为258是int对象, are immutable objects. So the values of the following 3 IDs are the same, the result of the last sentence is also true  #有点奇怪的是为什么在IDLE, and the result of the script execution is not quite the same. So the following code should be executed in the script.    Print (ID (258))  a = 258  print (ID (a))  B = 258  print (ID (b))  

Summarize the pros and cons of immutable objects.

The advantage is that this reduces the amount of memory space used by duplicate values.

The disadvantage is: I want to modify the value of this variable binding, if there is no memory block of this value in memory, then you must re-open a piece of memory, the new address and the variable name binding. Instead of modifying the value of the memory block that the variable originally pointed to, this gives a certain reduction in execution efficiency.

Second, Variable object

The content of its objects can vary. When the content of an object changes, the object reference of the variable does not change. As in the following example:

M=[5,9]  

function parameters

Python function parameters for mutable objects, changes in the parameters within the function affect the original object , and for non-mutable objects, changes to the parameters within the function do not affect the original parameters . The reason is:

1, variable object, parameter change is variable object, its content can be modified.

2, immutable object, change is the object of the variable within the function.

A brief introduction to mutable objects and immutable objects is presented here.

Recycling mechanism for objects

Python does not need to explicitly reclaim space occupied by objects like C, and the Python kernel has a garbage collection mechanism that is processed by the garbage collection mechanism when an object is unreachable.

Some objects refer to external resources, such as open files or Windows. Usually we think that when these objects are reclaimed by the garbage collection mechanism, the external resources that they occupy are freed. However, the garbage collection mechanism does not necessarily reclaim these objects, so these objects provide explicit methods (usually _close()_ ) to free external resources. It is best to use explicit methods to release external resources in the program, which can generally be _try...finally_ easily released.

Type of Object

The type of the object almost affects all of the functionality of the object, and in some ways the object's identifier is also affected by its type.

>>> sum = 15>>> Sum_add = + 3>>> sum is sum_addtrue>>> sum = 15000000>>> s Um_add = 10000000 + 5000000>>> sum is sum_addfalse>>> sum_add = = Sumtrue

For non-mutable objects (this is int), when we need a new object (Sum_add = 12 + 3), Python may return a reference to an object (sum) that already exists with a type and a value that is consistent. Of course, it is only possible to return objects that already exist, depending on the specific implementation of Python. The same is the creation of a new object Sum_add = 10000000 + 5000000,python does not return a sum of the same value and type to Sum_add.

>>> value = []>>> Value_1 = []>>> value is value_1false>>> value = = valuetrue>>& Gt Value = Value_1 = []>>> value is value_1true

For mutable objects, when we need new objects, Python will definitely create a new one for us. Note that value = value_1 = [] an empty list object will be created here, and then returned to both value and value_1.

The bewildering immutable object

First look at the following code: see how I change the value of immutable objects

>>> mutability = [1, 2, 3, 4]>>> immutability = (0, mutability, 5) >>> immutability (0, "1, 2, 3, 4], 5) #查看可变对象与不可变对象的标识符 (memory address) >>> ID (mutability) 28356200>>> ID (immutability) 28048640# Make some changes to mutable objects and immutable objects >>> mutability[2] = "See here!" >>> immutability (0, [1, 2, ' See here! ', 4], 5) #查看改变后的可变对象与不可变对象的标识符 (memory address) >>> ID (mutability) 28356200 >>> ID (immutability) 28048640

Here an element in the tuple immutability is a mutable object list mutability, and when we change the value of mutability, the value of the tuple called the immutable object seems to have changed. Why is that?

Before you answer this question, summarize what happened to the above code: immutable Object A contains a reference to variable object B, and the value of mutable object B may change when the value of the variable is changed.

So why do we still think that a is an immutable object? Since a still contains object B, and B's identifier does not change, the identifier for all elements of a does not change .

See what the layout of the memory is:

You can see that a is an object of list type, it is a mutable object, so modifying its value does not create a new object. But B is an immutable object, so long as it modifies its value, it re-creates the object, noting that there is no change in its value because the identifiers of all its elements are unchanged.

Let's look at the following example:

>>> a = 2>>> B = (1, a, 3) >>> B (1, 2, 3) >>> ID (a) 23112444>>> ID (b) 27983104> ;>> A + = 10>>> B (1, 2, 3) >>> ID (b[1]) 23112444>>> ID (b) 27983104>>> ID (a) 23112324

The memory layout before executing the a+=10 is:

After executing the a+=10 instruction, the memory layout is:

Here is just the original int (2) The number of references to this object minus 1.

In a ruthless, on the basis of the above code to another:

>>> B (1, 2, 3) >>> b[1] = 13Traceback (most recent call last):  File "<stdin>", line 1, in <m Odule>typeerror: ' Tuple ' object does not support item assignment>>> b[1]2

Assigning a value to an int type object through object B of the tuple type, so that if int is an immutable object, it will re-request the new memory, then it will also cause the change of the XXX reference variable in tuple B, so it really changes the identifier of an element of tuple B object. Causes a real change to the immutable tuple object B, at which point an error is made.

The basis of Python-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.