Python advanced 09 Dynamic type

Source: Internet
Author: User

Dynamic type (typing) is another important core concept of Python. As we said before, the python variable (variable) does not need to be declared, and when assigned, the variable can be re-assigned to any value. These are related to the concept of dynamic types.

Dynamic Type

In the objects we touch, there is a special kind of object that is used to store the data. Common objects of this class include various numbers, strings, tables, and dictionaries. In the C language, we call these data structures variables. In Python, these are objects.

An object is an entity that is stored in memory. But we don't have direct access to that object. The object name that we write in the program is just a reference to the object (reference).

The separation of references and objects is the core of the dynamic type. The reference can point to a new object at any time:

A = 3'at'

In the first statement, 3 is an integer object that is stored in memory. by assigning a value, reference a points to object 3.

The second statement, in memory, establishes the object ' at ', which is a string. Reference a points to ' at '. At this point, Object 3 no longer has a reference to it. Python automatically destroys objects that are not referenced to (destruct) and frees the corresponding memory.

(For small integers and short strings, Python caches these objects, rather than creating and destroying them frequently.) )

A = 5== a + 2

Look at this example again. With the first two sentences, we let A, B point to the same integer object 5 (B = A means to have reference B point to the object referred to by a). But the third sentence actually re-assigns a value to reference a, so that a points to a new object 7. At this point, A/b points to different objects. We see that even if multiple references point to the same object, if a reference value changes, the reference is actually made to point to a new reference and does not affect the point of the other reference . In effect, each reference is independent and unaffected.

The same is true for other data objects:

L1 = [== 1

But be aware of the following

L1 = [= == Tenprint L2

In this case, we no longer assign a value to the L1 reference, but instead assign a value to the element of the table to which L1 points. As a result, L2 also changed.

What is the reason? Because the direction of L1,L2 has not changed, it still points to that table. The table is actually an object that contains multiple references (each of which is an element, such as l1[0],l1[1] ..., each of which points to an object, such as a. and l1[0] = 10 This assignment operation, does not change the direction of the L1, but to L1[0], which is a part of the Table object (an element), so all references to the object are affected.

(In contrast, our previous assignment didn't work on the object itself, only changing the reference point.) )

A list can alter the object itself (in-place change) by referencing its elements. This type of object, called a mutable data object (mutable object), is the same type of data as the dictionary.

And like the numbers and strings before, you cannot change the object itself, only the reference point is changed, called the Immutable data object (immutable objects).

The tuple (tuple) we learned earlier, although it is possible to invoke a reference element but not to assign a value, cannot alter the object itself, so it is also considered immutable object.

the parameter passing of function from the view of dynamic type

The argument of a function is passed, essentially a reference. For example:

def f (x):     =    print= 1f (a)print A

The parameter x is a new reference to the object referred to by a. If the argument is an immutable (immutable) object, the A and x references are independent of each other. The operation on parameter x does not affect reference a. Such a pass is similar to the value passed in the C language.

If you pass a variable (mutable) object, changing the function parameter may change the original object. All references to the original object are affected, so be aware of the problem when programming. For example:

def f (x):     = +    print= []f (a)print A

Python advanced 09 Dynamic type

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.