代碼在運行伊始,建立的存儲「變量名與值的關係」的空間叫作全局命名空間,在函數的運行中開闢的臨時的空間叫作局部命名空間html
命名空間和做用域python
命名空間的本質:存放名字與值的綁定關係編程
1 >>> import this 2 The Zen of Python, by Tim Peters 3 4 Beautiful is better than ugly. 5 Explicit is better than implicit. 6 Simple is better than complex. 7 Complex is better than complicated. 8 Flat is better than nested. 9 Sparse is better than dense. 10 Readability counts. 11 Special cases aren't special enough to break the rules. 12 Although practicality beats purity. 13 Errors should never pass silently. 14 Unless explicitly silenced. 15 In the face of ambiguity, refuse the temptation to guess. 16 There should be one-- and preferably only one --obvious way to do it. 17 Although that way may not be obvious at first unless you're Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, it's a bad idea. 21 If the implementation is easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- let's do more of those!
在python之禪中提到過:命名空間是一種絕妙的理念,讓咱們盡情的使用發揮吧!數組
命名空間一共分爲三種:網絡
全局命名空間閉包
局部命名空間app
內置命名空間less
*內置命名空間中存放了python解釋器爲咱們提供的名字:input,print,str,list,tuple...它們都是咱們熟悉的,拿過來就能夠用的方法。ide
三種命名空間之間的加載與取值順序:函數
加載順序:內置命名空間(程序運行前加載)->全局命名空間(程序運行中:從上到下加載)->局部命名空間(程序運行中:調用時才加載)
取值:
在局部調用:局部命名空間->全局命名空間->內置命名空間
在局部使用變量取值狀況
1 x = 1 2 def f(x): 3 print(x) 4 5 print(10)
在全局調用:全局命名空間->內置命名空間
在全局引用變量x
1 x = 1 2 def f(x): 3 print(x) 4 5 f(10) 6 print(x)
在全局引用內置max
1 print(max)
做用域
做用域就是做用範圍,按照生效範圍能夠分爲全局做用域和局部做用域。
全局做用域:包含內置名稱空間、全局名稱空間,在整個文件的任意位置都能被引用、全局有效
局部做用域:局部名稱空間,只能在局部範圍內生效
globals和locals方法
在全局引用globals和locals
1 print(globals()) 2 print(locals()) 3 4 C:\Users\panhw\AppData\Local\Programs\Python\Python36\python.exe C:/Users/panhw/Desktop/學習文檔/python/day15.py 5 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000023BB77CA4A8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/panhw/Desktop/學習文檔/python/day15.py', '__cached__': None, 'sys': <module 'sys' (built-in)>, 'li': [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88], 'count': 1} 6 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000023BB77CA4A8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/panhw/Desktop/學習文檔/python/day15.py', '__cached__': None, 'sys': <module 'sys' (built-in)>, 'li': [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88], 'count': 1}
在局部引用globals和locals
1 def func(): 2 a = 12 3 b = 20 4 print(locals()) 5 print(globals()) 6 7 func() 8 9 10 C:\Users\panhw\AppData\Local\Programs\Python\Python36\python.exe C:/Users/panhw/Desktop/學習文檔/python/day15.py 11 {'b': 20, 'a': 12} 12 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001FEE5D2A4A8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/panhw/Desktop/學習文檔/python/day15.py', '__cached__': None, 'sys': <module 'sys' (built-in)>, 'li': [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88], 'count': 1, 'func': <function func at 0x000001FEE4123E18>}
global 關鍵字
1 a = 10 2 def func(): 3 global a 4 a = 20 5 6 print(a) 7 func() 8 print(a)
函數的嵌套和做用域鏈
函數的嵌套調用
1 def max2(x,y): 2 m = x if x>y else y 3 return m 4 5 def max4(a,b,c,d): 6 res1 = max2(a,b) 7 res2 = max2(res1,c) 8 res3 = max2(res2,d) 9 return res3 10 11 # max4(23,-7,31,11)
函數的嵌套定義
1 def f1(): 2 print("in f1") 3 def f2(): 4 print("in f2") 5 6 f2() 7 f1()
函數的做用域鏈
1 def f1(): 2 a = 1 3 def f2(): 4 print(a) 5 f2() 6 7 f1()
1 def f1(): 2 a = 1 3 def f2(): 4 def f3(): 5 print(a) 6 f3() 7 f2() 8 9 f1()
1 def f1(): 2 a = 1 3 def f2(): 4 a = 2 5 f2() 6 print('a in f1 : ',a) 7 8 f1()
nonlcoal 關鍵字
1.外部必須有這個變量
2.在內部函數聲明nonlocal變量以前不能再出現同名變量
3.內部修改這個變量若是想在外部有這個變量的第一層函數中生效
1 def f1(): 2 a = 1 3 def f2(): 4 nonlocal a 5 a = 2 6 f2() 7 print('a in f1 : ',a) 8 9 f1()
函數名的本質
函數名本質上就是函數的內存地址
1.能夠被引用
1 def func(): 2 print('in func') 3 4 f = func 5 print(f)
2.能夠被看成容器類型的元素
1 def f1(): 2 print('f1') 3 4 5 def f2(): 6 print('f2') 7 8 9 def f3(): 10 print('f3') 11 12 l = [f1,f2,f3] 13 d = {'f1':f1,'f2':f2,'f3':f3} 14 #調用 15 l[0]() 16 d['f2']()
3.能夠看成函數的參數和返回值
1 第一類對象(first-class object)指 2 1.可在運行期建立 3 2.可用做函數參數或返回值 4 3.可存入變量的實體。
閉包
閉包函數:
內部函數包含對外部做用域而非全劇做用域名字的引用,該內部函數稱爲閉包函數
函數內部定義的函數稱爲內部函數
在計算機科學中,閉包(Closure)是詞法閉包(Lexical Closure)的簡稱,是引用了自由變量的函數。這個被引用的自由變量將和這個函數一同存在,即便已經離開了創造它的環境也不例外。因此,有另外一種說法認爲閉包是由函數和與其相關的引用環境組合而成的實體。閉包在運行時能夠有多個實例,不一樣的引用環境和相同的函數組合能夠產生不一樣的實例。
1 def func(name): 2 def inner_func(age): 3 print 'name:', name, 'age:', age 4 return inner_func 5 6 bb = func('the5fire') 7 bb(26) # >>> name: the5fire age: 26
閉包函數最經常使用的用法
1 def func(): 2 name = 'eva' 3 def inner(): 4 print(name) 5 return inner 6 7 f = func() 8 f()
判斷閉包函數的方法__closure__
1 #輸出的__closure__有cell元素 :是閉包函數 2 def func(): 3 name = 'eva' 4 def inner(): 5 print(name) 6 print(inner.__closure__) 7 return inner 8 9 f = func() 10 f() 11 12 #輸出的__closure__爲None :不是閉包函數 13 name = 'egon' 14 def func2(): 15 def inner(): 16 print(name) 17 print(inner.__closure__) 18 return inner 19 20 f2 = func2() 21 f2()
閉包嵌套
1 def wrapper(): 2 money = 1000 3 def func(): 4 name = 'eva' 5 def inner(): 6 print(name,money) 7 return inner 8 return func 9 10 f = wrapper() 11 i = f() 12 i()
閉包函數獲取網絡應用
1 from urllib.request import urlopen 2 3 def index(): 4 url = "http://www.xiaohua100.cn/index.html" 5 def get(): 6 return urlopen(url).read() 7 return get 8 9 xiaohua = index() 10 content = xiaohua() 11 print(content)
命名空間:
一共有三種命名空間從大範圍到小範圍的順序:內置命名空間、全局命名空間、局部命名空間
做用域(包括函數的做用域鏈):
小範圍的能夠用大範圍的
可是大範圍的不能用小範圍的
範圍從大到小(圖)
在小範圍內,若是要用一個變量,是當前這個小範圍有的,就用本身的
若是在小範圍內沒有,就用上一級的,上一級沒有就用上上一級的,以此類推。
若是都沒有,報錯
函數的嵌套:
嵌套調用
嵌套定義:定義在內部的函數沒法直接在全局被調用
函數名的本質:
就是一個變量,保存了函數所在的內存地址
閉包:
內部函數包含對外部做用域而非全劇做用域名字的引用,該內部函數稱爲閉包函數
2. 爲何使用閉包
基於上面的介紹,不知道讀者有沒有感受這個東西和類有點類似,類似點在於他們都提供了對數據的封裝。不一樣的是閉包自己就是個方法。和類同樣,咱們在編程時常常會把通用的東西抽象成類,(固然,還有對現實世界——業務的建模),以複用通用的功能。閉包也是同樣,當咱們須要函數粒度的抽象時,閉包就是一個很好的選擇。
在這點上閉包能夠被理解爲一個只讀的對象,你能夠給他傳遞一個屬性,但它只能提供給你一個執行的接口。所以在程序中咱們常常須要這樣的一個函數對象——閉包,來幫咱們完成一個通用的功能,好比後面會提到的——裝飾器。
3. 使用閉包
第一種場景 ,在python中很重要也很常見的一個使用場景就是裝飾器,Python爲裝飾器提供了一個很友好的「語法糖」——@,讓咱們能夠很方便的使用裝飾器,裝飾的原理不作過多闡述,簡言之你在一個函數func上加上@decorator_func, 就至關於decorator_func(func):
1 def decorator_func(func): 2 def wrapper(*args, **kwargs): 3 return func(*args, **kwargs) 4 return wrapper 5 6 @decorator_func 7 def func(name): 8 print 'my name is', name 9 10 # 等價於 11 decorator_func(func)