1. 程式人生 > >13 Python 函數進階

13 Python 函數進階

desktop peter spl 語法糖 自由 font close bad 場景

代碼在運行伊始,創建的存儲“變量名與值的關系”的空間叫做全局命名空間,在函數的運行中開辟的臨時的空間叫做局部命名空間

命名空間和作用域

  命名空間的本質:存放名字與值的綁定關系

技術分享
 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 arent 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 youre Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, its a bad idea. 21 If the implementation is
easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- lets do more of those!
View Code

在python之禪中提到過:命名空間是一種絕妙的理念,讓我們盡情的使用發揮吧!

命名空間一共分為三種:

  全局命名空間

  局部命名空間

  內置命名空間

*內置命名空間中存放了python解釋器為我們提供的名字:input,print,str,list,tuple...它們都是我們熟悉的,拿過來就可以用的方法。

三種命名空間之間的加載與取值順序:

加載順序:內置命名空間(程序運行前加載)->全局命名空間(程序運行中:從上到下加載)->局部命名空間(程序運行中:調用時才加載)

取值:

  在局部調用:局部命名空間->全局命名空間->內置命名空間

  在局部使用變量取值情況

技術分享
1 x = 1
2 def f(x):
3     print(x)
4 
5 print(10)
View Code

  在全局調用:全局命名空間->內置命名空間

  在全局引用變量x

技術分享
1 x = 1
2 def f(x):
3     print(x)
4 
5 f(10)
6 print(x)
View Code

在全局引用內置max 

技術分享
1 print(max)
View Code

作用域

  作用域就是作用範圍,按照生效範圍可以分為全局作用域和局部作用域。

  全局作用域:包含內置名稱空間、全局名稱空間,在整個文件的任意位置都能被引用、全局有效

  局部作用域:局部名稱空間,只能在局部範圍生效

  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}
View Code

  在局部引用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>}
View Code

  global 關鍵字

技術分享
1 a = 10
2 def func():
3     global a
4     a = 20
5 
6 print(a)
7 func()
8 print(a)
View Code

函數的嵌套和作用域鏈

  函數的嵌套調用

技術分享
 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)
View Code

  函數的嵌套定義

技術分享
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()
View Code

函數名的本質

  函數名本質上就是函數的內存地址

  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.可存入變量的實體。
View Code

閉包

  閉包函數:

    內部函數包含對外部作用域而非全劇作用域名字的引用,該內部函數稱為閉包函數
    函數內部定義的函數稱為內部函數

  在計算機科學中,閉包(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
View Code

  閉包函數最常用的用法

技術分享
1 def func():
2     name = eva
3     def inner():
4         print(name)
5     return inner
6 
7 f = func()
8 f()
View Code

  判斷閉包函數的方法__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()
View Code

  閉包嵌套

技術分享
 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()
View Code

  閉包函數獲取網絡應用

技術分享
 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)
View Code

命名空間:

  一共有三種命名空間從大範圍到小範圍的順序:內置命名空間、全局命名空間、局部命名空間

作用域(包括函數的作用域鏈):

小範圍的可以用大範圍的
但是大範圍的不能用小範圍的
範圍從大到小(圖)

技術分享

在小範圍內,如果要用一個變量,是當前這個小範圍有的,就用自己的
如果在小範圍內沒有,就用上一級的,上一級沒有就用上上一級的,以此類推。
如果都沒有,報錯

函數的嵌套:

  嵌套調用

  嵌套定義:定義在內部的函數無法直接在全局被調用

函數名的本質:

  就是一個變量,保存了函數所在的內存地址

閉包:

  內部函數包含對外部作用域而非全劇作用域名字的引用,該內部函數稱為閉包函數

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)
View Code

13 Python 函數進階