Dynamic typing for advanced python tutorials

  • 2020-04-02 14:02:45
  • OfStack

Dynamic typing is another important core concept in Python. As we've said before, Python variables don't need to be declared, and when assigned, variables can be reassigned to any value. These are all related to the concept of dynamic typing.

Dynamic type

Among the objects we touch, there is a special class of objects for storing data. Common examples of this class include Numbers, strings, tables, and dictionaries. In C, we call these data structures variables. In Python, these are objects.

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

The separation of references and objects is at the heart of dynamic typing. A reference can always point to a new object:


a = 3
a = 'at'

In the first statement, 3 is an integer object stored in memory. By assignment, reference a points to object 3.

In the second statement, the object 'at' is created in memory, which is a string. The reference a points to 'at'. At this point, object 3 no longer has a reference to it. Python will automatically destroy (destruct) an object to which there is no reference, freeing the appropriate memory.

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

a = 5
b = a
a = a + 2

Let's look at this example. With the first two sentences, we have a and b pointing to the same integer object 5(b = a means that b points to the object that a refers to). But the third sentence actually reassures the reference a to point to a new object 7. Where a and b point to different objects. We see that even if multiple references point to the same object, if the value of one reference changes, you are actually making the reference point to a new reference without affecting the direction of the other references. In effect, each reference is independent of each other.

The same is true for other data objects:


L1 = [1,2,3]
L2 = L1
L1 = 1

But be aware of the following


L1 = [1,2,3]
L2 = L1
L1[0] = 10
print L2

In this case, instead of assigning a reference to L1, we assign a value to the element of the table that L1 points to. As a result, L2 also changes at the same time.

Why? Because the direction of L1 and L2 hasn't changed, it still points to that table. Tables are actually objects that contain multiple references (each reference is an element, such as L1[0], L1[1]... , each reference points to an object, such as 1,2,3),. The assignment of L1[0] = 10 does not change the direction of L1, but operates on L1[0], which is part of the table object (an element), so all references to that object are affected.

(in contrast, none of our previous assignments acted on the object itself, just changing the reference.)

A list can change the object itself by referencing its elements. This object type, known as variable data object (mutable object), the dictionary is the data type.

Like the previous Numbers and strings, the object itself cannot be changed, only the reference point can be changed, called immutable object.

The tuple we learned earlier, although you can call a reference element, you cannot assign a value, so you cannot change the object itself, so it is immutable object.

The argument passing of a function from dynamic type

Passing arguments to a function, essentially passing a reference. Such as:


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

Parameter x is a new reference to the object referred to by a. If the parameter is immutable, 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 that in C.

If the transfer is variable (mutable) object, then change the function parameters, could change the original object. All references to the original object are affected, so be aware of this when programming. Such as:


def f(x):
    x[0] = 100
    print x a = [1,2,3]
f(a)
print a

Dynamic typing is one of Python's core mechanisms. You can get used to it in the application.

conclusion

Separation of references from objects, which are entities that store data in memory, and references to objects.

Mutable object, immutable object

Function value transfer


Related articles: