Python scope
- Python no block-level scope to see the C language code:
#include <stdio.h>int main () { if(20) { int0; } printf ("i =%d", i); return 0 ;}
In this code, if clause introduces a local scope, the variable i exists in this local scope, but is not visible externally, so the next reference to the variable i in the printf function throws a compilation error, but not in Python
Look at the following code:
if 1 = =1 :'fuzj'print(name)
In this code, the IF clause does not introduce a local scope, and the variable i is still in the global scope, so the variable i is visible to the next print statement
Therefore, Python has no block-level scope.
- Python scopes are functions, classes, modules
Code:
def F1 (): ' Fuzj ' Print (name) # Print Error
The function F1 has isolated the variable scope of name, so the name variable is not found when you print outside the function
- Python's scope chain lookup order is to find out in the scope of Python from inside out of the outside, that is, to find the local variables within the function, and then look for global variables, until the error is not found, see the following example,
' Fuzj ' def F1 (): ' Jie ' Print (name) F1 () output is Jie
- Python's scope has been determined before executing the Python interpreter after the code has been interpreted, the scope of the code has been determined, when this function is called, will first find its own scope, and then find the global
Name ='Fuzj'defF1 ():Print(name)defF2 (): Name='Jie'F1 () f2 () Output: Fuzjname='Fuzj'defF1 ():Print(name)defF2 (): Name='Jie' returnF1res=F2 () res () output is Fuzj
The above example shows that after the Python interpreter has been interpreted, the scope of the F1 has been confirmed, the scope chain is confirmed, the same F2 function has been confirmed, so at the time of execution, which function is called to execute that function, to find the previously defined scope
The case of the bombing day
for in range ( x) # Special syntax, the for loop generates the elements of the list, and finally makes up a list of the number within the for loop of 10, and gives each X. plus 1, the final form of the Li list print (LI) [1, 2, 3, 4, 5, 6, 7, 8, 9,]for inif x>7] # Add the judging condition, X is greater than 7 1print(LI) [9, 10]
Understand the above bedding, please see the following case
>>> Li = [Lambda: X forXinchRange (10)]>>>Print(LI) [<function <listcomp>.<Lambda> at 0x101bdbae8>, <function <listcomp>.<Lambda> at 0x101bdbb70>, <function <listcomp>.<Lambda> at 0x101bdbbf8>, <function <listcomp>.<Lambda> at 0x101bdbc80>, <function <listcomp>.<Lambda> at 0x101bdbd08>, <function <listcomp>.<Lambda> at 0x101bdbd90>, <function <listcomp>.<Lambda> at 0x101bdbe18>, <function <listcomp>.<Lambda> at 0x101bdbea0>, <function <listcomp>.<Lambda> at 0x101bdbf28>, <function <listcomp>.<Lambda> at 0x101bec048>]>>>Print(li[0])<function <listcomp>.<Lambda> at 0x101bdbae8>>>>Print(Li[0] ())9>>>Print(li[1]())9
Have you been ignorant of B?
Look at the following explanation:
- 1. First Li is a list
- The 2.LAMBDA expression is actually a simple function, before the semicolon is the argument, the semicolon is the return expression, in this case, the lambda returns the value of X
- 3. Based on the above, the lambda expression of the elements in the Li list, namely li = [lambda:x,lambda:x ....]
- 4. The function's internal code is not executed until it executes, so the Li is printed, and the lambda expression object is output
- 5. Function parentheses indicate execution function
- The first element in 6.li is a function, so li0 represents the execution of the first function, at which point the function is actually started, and the lambda expression does not define the x parameter, so take the result of the For loop and finally get 9.
Python Multiple inheritance
python2.7 and Python3 classes are different, python2.7 class to the classic class and the new class, Python3 are all new classes, they are multiple inheritance, the class, the inheritance order is different
- Multiple inheritance of the Python3 class
For details, please click here: http://www.cnblogs.com/pycode/p/class.html
- python2.7 Multiple Inheritance
No inheritance other classes are classic classes
class Foo: Pass
There are inherited other parent classes, or the object class, which is the new class
class Foo (object): Pass
?
?
If a Python class inherits more than one class, there are two ways to find it: depth first and breadth first
?
When a class is a classic class, multiple inheritance cases are searched in the depth-first way
When a class is a new class, in multiple inheritance cases, the breadth-first method is found
Case
-
- Classic class Multiple Inheritance
classD:defF1 (self):Print("d.f1")classB (D):deff (self):Print("b.f1")classC (D):defF1 (self):Print("c.f1")classA (B, C):deff (self):Print("A.F") A=A () a.f1 () Output: D.F1 because Class D is a classic class where both B and C inherit d,a inheritance C and B,ABC are modern classes, the result is a Class D F1 based on the inheritance rules of depth precedence .
-
- New class multiple Inheritance
classD (object):defF1 (self):Print("d.f1")classB (D):deff (self):Print("b.f1")classC (D):defF1 (self):Print("c.f1")classA (B, C):deff (self):Print("A.F") A=A () a.f1 () output result: C.F1 Because Class D inherits object, ABCD is a new class. According to the principle of finding breadth first, the final output is c.f1.
Python scopes and multiple inheritance