Python Learning Manual notes on dynamic type

Source: Internet
Author: User

Reprint Please specify: theviper http://www.cnblogs.com/TheViper

Variables, objects, and references

In Python, types are automatically determined during the run, not through code declarations. This means that there is no need to declare variables beforehand.

The process of a=3 parsing:

    1. Variable A: Here, the variable A, when the code assigns it the first time, creates it, and then assigns the value of the variable that was created.
    2. Variable type: A variable never has any type information or constraints associated with it. The concept of a type exists in an object rather than a variable. The variable is originally generic, it simply refers to a particular object at a specific point in time.
    3. Variable use: When a variable appears in an expression, it is immediately replaced by the currently referenced object, regardless of the type of object. Note that all variables must be explicitly assigned before they are used.

In summary, a variable is created when it is assigned, it can reference any type of object, and must be assigned before the reference.

The assignment operation of the above code:

    1. Creating an object (here is a number) represents the value 3.
    2. Create a variable A, if it has not yet been created.
    3. Connect the variable to the new object 3.

As shown, variables and objects are stored in different parts of memory, and they are associated by a connection (the arrow in the diagram).

Specifically, after running a=3, variable a becomes a reference to object 3. Internally, a variable is actually a pointer to the object's memory space (created by running the constant expression 3).

In terms of specific terminology,

    • A variable is an element of a system table and has a connection space to an object.
    • An object is an allocated piece of memory and has enough space to represent the value it represents.
    • A reference is an automatically formed pointer from a variable to an object.

In fact, Python does not re-request a new piece of memory space for all objects created. As an optimization, Python caches and re-uses objects that are immutable (such as numeric values with smaller numbers, strings, tuples, and so on).

The type belongs to the object, not the variable

A=3a='viper'a=1.333

The variable a starts with an integer, then a string, and finally a floating-point number, but this code can be run. This is because, in Python, the situation is simple and the variable name has no type.

The above code, we just change the variable A to a reference to different objects, so that variable a refers to different types of objects.

Shared references

A=3b=a

You can see that the variable A and a refers to the same object. At this point, let variable a refer to another object.

A=3b=aa='viper'

A= ' Viper ' creates a new object ' Viper ' and makes the variable a reference to the new object, while variable B still references the original object 3. So the value of variable B is 3.

Share references and modify in situ

Some objects and operations do change objects in place, for example, assigning a value to an offset in a list changes the list object instead of generating a new list object. Be careful when sharing references to objects that can be modified in place, because modifications to one variable name affect other variables that reference the same object.

l1=[1,2,3]l2=l1l1[0]=33

The value of the variable L1,L2 is then changed to [33,2,3].

This behavior is usually not what we want, but this behavior is the default, and if you don't want this behavior to happen, you need to copy the object instead of creating the reference.

l1=[1,2,3]l2=l1[:]l1[0]='viper'print(L2)#  [ [+]

Because L2 refers to a copy of the L1 reference object, there are two variables pointing to different memory regions. So the change of L1 will not affect L2.

Note that shards are used, and other mutable core types (such as dictionaries, collections) cannot be fragmented, and the copy () method is used.

Shared objects and equality

Because of the Python reference model, there are two different ways to check for equality in a Python program. = = and is

= = checks whether the two referenced objects have the same value, and this method is used as an equal check.

Is compares pointers that implement references and is a way to detect shared references. If the reference value of the variable is equal, but it is a different object, it returns false. For example,

Variables M and l have the same values, but they are two different objects in different memory regions.

Other than that

This verifies that Python will cache immutable objects (such as numeric numbers, strings, tuples, and so on) and reuse them. Y=3 just lets the variable y refer to object 3 that has already been created.

Python Learning Manual notes on dynamic type

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.