In Python, there are no types of variables, which are different from most editing languages that you've seen in the past. When using a variable, you do not need to declare it in advance, just assign a value to the variable. However, when you use a variable, you must assign a value to the variable, and if you write only one variable without assigning a value, Python does not think the variable is defined. As follows:
>>> Atraceback (most recent): File "
", line 1, in
nameerror:name ' a ' are not Defined
Let's talk about variables, references, copies, and scope issues in Python:
One, mutable objects & immutable objects
In Python, objects are divided into two types: mutable objects and immutable objects, immutable objects including Int,float,long,str,tuple, and mutable objects including list,set,dict. It is important to note that immutable refers to the immutable value of the variable. For variables of an immutable type, if you want to change the variable, a new value is created that binds the variable to the new value, and the old value waits for garbage collection if it is not referenced. In addition, the immutable type can calculate the hash value as the dictionary key. When mutable type data is applied to an object, it is not necessary to apply the memory elsewhere, just to request (+/-) continuously after this object, that is, its memory address will remain unchanged, but the area will grow or become shorter.
Here are some examples:
>>> a = ' xianglong.me ' >>> ID (a) 140443303134352>>> a = ' 1saying.com ' >>> ID (a) After the 140443303131776# is re-assigned, the memory address of variable A has changed # ' xianglong.me ' is the str type, immutable, so the assignment operation knowledge Re-creates the str ' 1saying.com ' object and then points the variable A to it
>>> a_list = [1, 2, 3]>>> ID (a_list) 140443302951680>>> a_list.append (4) >>> ID (a_ List) After the 140443302951680# list is re-assigned, the memory address of the variable a_list does not change # [1, 2, 3] is mutable, the append operation only changes its value, the variable a_list points to the unchanged
No type of variable, object has type
Third, function value transfer
Let's look at an example:
Def func_int (a): A + = 4 def func_list (a_list): a_list[0] = 4 T = 0func_int (t) print t# output:0 t_list = [1, 2, 3] Func_list (t_list) print t_list# output: [4, 2, 3]
For the above output, a lot of Python beginners are confused: The first example looks like a pass value, and the second example does pass a reference. In fact, it is also very easy to explain the problem, mainly because of mutable objects and immutable objects: for Mutable objects, the object's operations do not reconstruct the object, and for immutable objects, each operation rebuilds the new object.
When a function parameter is passed, Python is actually assigning a reference to the object corresponding to the variable that is passed in the argument to the function's internal variable. Referring to the above example to make it easier to understand that the local variable "a" in Func_int is actually another reference to the object pointed to by all variables "T", since the integer object is immutable, when Func_int changes the variable "a", it is actually the local variable "a" Points to the integer object "1". So obviously, Func_list modifies a mutable object, the local variable "a" and the global variable "t_list" point to the same object.
Four, shallow copy & deep copy
The next question is: What if we must replicate a copy of a mutable object? Simple assignment has proven to be infeasible, so Python provides a copy module specifically for copying mutable objects. There are two methods in copy: Copy () and Deepcopy (), the previous is a shallow copy, and the latter is a deep copy. A shallow copy copies only the first object passed to it, regardless of the following, while a deep copy copies all the objects that can be copied. Here is an example:
A = [[1, 2, 3], [4, 5, 6]]b = AC = Copy.copy (a) d = copy.deepcopy (a) a.append () a[1][2] = Print Aprint bprint cprint D # [[1, 2, 3], [4, 5, 10], 15]# [[1, 2, 3], [4, 5, 10], 15]# [[1, 2, 3], [4, 5, 10]]# [[1, 2, 3], [4, 5, 6]
V. Scope
When you create, change, or look up variable names in a Python program, you are in a place where you save the variable names, which we call namespaces. The term scope is also called namespaces. Specifically, the position of the variable name is assigned in the code (the variable declaration in Python is assigned, and global declares that only the variable's use domain) determines the scope in which the variable can be accessed. The function defines the local scope, and the module defines the global scope.
Each of these modules is a global scope. That is, the variable created at the top of the module file has a global scope, and for external access it becomes a property of the module object. The scope of the global scope is scoped to a single file only. "Global" refers to the top-level variable name in a file that is global to this file. Each call to a function creates a new local scope. Python also has recursion, which can call itself, creating five new local namespaces each time a call is made. The assigned variable name is a local variable unless it is declared as a global variable. If you need to assign a value to the variable name at the top level of the module file inside the function, you need to declare the variable inside the function through the global statement. All variables can be summarized as local, global, or built-in three kinds. The range is within the Def Interior, within the namespace of a module and the variables provided by the predefined __builtin__ module.
The variable name reference is divided into three scopes to look for: the first is local, then inside the function (if any), then the global, and finally the built-in. By default, variable name assignments Create or change local variables. A global declaration assigns a value to the variable name that is mapped to the scope inside the module file. Python's variable name resolution mechanism is also known as the LEGB rule, as follows:
When an indeterminate variable name is used in a function, Python searches for 4 scopes: local scope (L), followed by the local scope (E) of def or Lambda in the previous nested structure, followed by the global scope (G), and finally the built-in scope (B). Follow this search principle and stop at the first place found. If not found, Python will make an error. Describes the search process (internal and external):
It says that there are no types of variables in Python, but Python is actually type-sensitive: All of Python's variables are actually pointers to objects in memory and are references to values that follow the object. In summary: In Python, types are objects, not variables, variables and objects are detached, objects are entities that store data in memory, and variables are pointers to objects. In the book Learning Python, there is a view that there are no types of variables, objects have types, and that is probably the meaning of that. Here is a diagram that illustrates the variables:
Python provides a global syntax like PHP, and global defined local variables become an alias for their corresponding global variables, that is, the same variable. The following example can help you understand better:
A = 44 def test1 (): a = print atest1 () # Output: Def test2 (): Global a print atest2 () # Output: