Python variables in memory mechanism

Author: Vamei Source: http: //www.cnblogs.com/vamei welcome to reprint, please keep this statement. Thank you!

Thank TeaEra, kitty cat

Dynamic type (dynamic typing) is another important core concepts of Python. We have said before, Python variables (variable) need not be declared, while on assignment, variables can be reassigned to any value. These are related to the concept of dynamic type.

Dynamic type
object in contact with us, a special class of objects, for storing data. Common Object class includes a variety of numbers, strings, tables, dictionary. In the C language, we call this some of the data structure is variable. In Python, these are objects.

Entity objects are stored in memory. But we can not directly come into contact with the object. We write in the program object name, just point to the object reference (reference).

Reference and object separation, the core of the dynamic type. Reference can point to a new object at any time:

3 = A
A = 'AT'
first statement 3 is an Integer object stored in memory. By assigning, a reference to an object 3.

The second sentence in the memory to create an object 'at', a character string (string). A reference point to the 'at'. In this case, the object 3 will not have any reference. Python will automatically no references to the object is destroyed (destruct), release the corresponding memory.

(For small integers and short strings, Python caches these objects, rather than frequent the establishment and destruction.)

. 5 = A
B = A
A = A + 2
look at this example. By the first two sentences, we let a, b point to the same Integer object 5 (b = a b meaning is to make a reference to an object that references a point referred to). However, the third sentence is actually a reference to reassign a, make a point to a new object 7. At this time, a, b point to different objects. We see that even multiple references point to the same object if a reference value changes, so this is actually a reference point to make a new reference point does not affect the other references. In effect, each reference is independent of each other.

Other data objects is also true:

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

However, note the following

= L1 [l, 2,3]
L2 of L1 =
L1 [0] = 10
Print L2 of
In this case, we are no longer referenced L1 this assignment, but for a table element pointed assignment L1. As a result, L2 changes too.

Why? Because L1, L2 point has not changed, still pointing to that table. An object table actually contains a plurality of references (each reference is an element, such as L1 [0], L1 [1] ..., each reference to an object, such as 1,2,3). And L1 [0] = 10 This assignment is not changed point L1, L1 but to [0], that is, a part (an element of) an object table, an operation, all point to the object reference They are affected.

(In contrast, our previous assignment did not play a role on the object itself, just change the reference point.)

By reference in its list of elements, changing the object itself (in-place change). Object of this type, called a variable data object (mutable object), the dictionary is such a data type.

And the numbers and strings as before, the object itself can not be changed, only changing the reference point, referred to as immutable data object (immutable object).

Tuple (tuple) before we learn, although you can call the reference element, but not the assignment, and therefore can not change the object itself, so it can be considered immutable object.

See parameter transfer function from the dynamic type
parameters of the transfer function, the transfer essentially by reference. For example:

Press Ctrl + C to copy the code

def f(x):
x = 100
print x

. 1 = a
F (a)
Print a
duplicated code Press Ctrl + C
the parameter x is a new reference, the meaning of a pointing object. If the parameter is not changed (the immutable) object, a and x are independent reference. Operation does not affect the reference parameter x a. This value is passed in the transfer similar to the C language.

If the transmission is variable (the mutable) object, then changing function parameters, it is possible to change the original object. All references point to the original object will be affected, programming time to pay attention to this issue. For example:

Press Ctrl + C to copy the code

def f(x):
x[0] = 100
print x

= A [l, 2,3]
F (A)
Print A
duplicated code Press Ctrl + C

Python's dynamic typing is one of the core mechanism. Can slowly familiar applications.

Summary
reference and object separation, the objects are physical memory storing data, a reference to an object.

Mutable object, immutable objects

Transfer function values

Published 13 original articles · won praise 18 · views 810

Guess you like

Origin blog.csdn.net/qzonelaji/article/details/104076020