Python's scope differs from other languages, and he has several tips to determine the scope.
1. Python does not have a block-level scope, only the scope of the function level.
For example, in the following example, if it is Java or other languages, the error name is not defined. In Python, because there is no block-level scope, name directly outputs 9.
>>> for I in range: Name=iprint (name)----------9
2. The order of the scope chain is to be looked up from within until it is not found
For example
>>> def f1 (): Name= ' A ' def f2 (): name= ' B ' Print (name) f2 () print (name) F1 ()------------- -ba
3. The scope has been determined before the function is executed, and the scope chain has been determined.
For example, before F0 () was executed, it was determined that name was Alex, so no matter how he called, the output was always Alex
>>> name= ' Alex ' def F0 (): Print (name) def F1 (): Name= ' BBB ' F0 () def f2 (): Name= ' Eric ' F1 () F2 ()------- ----------Alex.
Here are a few special examples and syntax
For example, we can assign a value to a list in the following way
>>> li=[x+100 for x in range]print (LI) li=[x+100 for x in range if X>6]print (LI)------------[100, 101, 102, 103, 104, 105, 106, 107, 108, 109][107, 108, 109]
If the above example is changed to a lambda expression, because lambda is actually a function, he will not execute the internal code before execution, so every element in the list is a function, and when we () to execute it, he will try to return X, And x this time after 10 cycles is already 9.
>>> #li列表里面的元素 [Functions, Functions, functions], the internal code does not execute Li=[lambda :x for x in range until the function is executed ( ]print (LI) print (li[0] ())-----------------[<function <listcomp>.<lambda> at 0x0000019ea4515bf8>, <function <listcomp>.<lambda> at 0x0000019ea4515c80 >, <function <listcomp>.<lambda> at 0x0000019EA4515D08>, < function <listcomp>.<lambda> at 0x0000019ea4515d90>, <function < listcomp>.<lambda> at 0x0000019ea4515e18>, <function <listcomp>.< Lambda> at 0x0000019ea4515ea0>, <function <listcomp>.<lambda> at 0x0000019EA4515F28>, <function <listcomp>.<lambda> at 0x0000019ea452b048>, <function <listcomp>.<lambda> at 0x0000019ea452b0d0 >, <function <listcomp>.<lambda> at 0x0000019ea452b158>]9
For an easy-to-understand notation.
>> li=[]for i in range: Def F1 (): Return I li.append (F1) #li是列表, inner elements are functions of the same function #i=9print (i) # # F1 not called before , the internal code does not execute; So li inside is a function # #真正执行 (), he will return I, the interior without I, looking up, I is already 9, so return 9print (Li[0] ()) print (Li[2] ()) 99
If you change to X=I,F1 initialization, you specify the value of x.
>>> li=[]for i in range: Def F1 (x=i): return x li.append (F1) #li是列表, inner elements are functions of the same function #iprint (i) # # F1 No There are calls before the internal code does not execute; So li inside is a function # #真正执行 (), he will return I, internal no I, looking up, I is already 9, so return 9print (Li[0] ()) print (Li[2] ()) 902
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1861879
Python Learning notes-scope trivia