python 函數基礎2,python函數基礎
一、什麼是命名關鍵字參數?
格式: 在*後面參數都是命名關鍵字參數。
特點:1、約束函數的調用者必須按照Kye=value的形式傳值。
2,、約束函數的調用者必須用我們指定的Key名。
def auth(*args,name,pwd): print(name,pwd)auth(pwd='213',name='egon')def register(name,age): print(type(name),type(age))register(123,[1,2,3])
###以上輸出:
egon 213
<class 'int'> <class 'list'>
二、函數的嵌套
1、函數的嵌套調用:在函數內又調用了其他函數
def max2(x,y): if x > y: return x else: return ydef max3(x,y,z): res1=max(x,y) res2=max(res1,z) return res2print(max3(88,99,100))
###以上輸出:
100
2、函數的嵌套定義:在函數內又定義其他函數。
def func1(): print('from func1') def func2(): #func2=記憶體位址 print('from func2') print(func2) #<function func1.<locals>.func2 at 0x0000024907A098C8> func2() func2() func2()func1()
###以上輸出:
from func2
from func2
from func2
三、函數的名稱空間
1、名稱空間:存放名字與值綁定關係的地方
x=888888888def func(): pass
2、名稱空間分為三類
(1)內建名稱空間:存放python解譯器內建的名字,在解譯器啟動時就生效,解譯器關閉則失效。
(2)全域名稱空間:檔案層級的名字,在執行檔案的時候生效,在檔案結束或者在檔案執行期間被刪除則失效。
x=1def f1(): def f2(): print(x) f2()f1()if 10 > 3: y=33333while True: xxxxx=123123123
###以上輸出:
1
(3)局部名稱空間:存放函數自訂的名字(函數的參數以及函數內的名字都存放與局部名稱空間),在函數調用時臨時生效,函數結束則失效。
注意:
載入順序:內建名稱空間-------->>全域名稱空間------->>>局部名稱空間
尋找名字:局部名稱空間-------->>全域名稱空間------->>>內建名稱空間
def f1(): # len=1 def f2(): # len=2 print(len) f2()f1()
###以上輸出:
global
3、範圍
全域範圍:包涵的是內建名稱空間與全域名稱空間的名字。
特點:
(1)在任何位置都能夠訪問的到
(2)該範圍內的名字會伴隨程式整個生命週期
局部範圍:包含的是局部名稱空間的名字
特點:
(1)只在函數內使用
(2)調用函數時生效,調用結束失效
四、函數對象
1、函數在python中是第一類對象
(1)可以被引用
x=1y=xdef bar(): print('from bar')f=barf()
###以上輸出:
from bar
(2)可以當做參數傳入
x=1def func(a): print(a)func(x)
###以上輸出:
1
(3)可以當做函數的傳回值
#代碼(1)
x=1def foo(): return xres=foo()print(res)
###以上輸出:
1
代碼(2)
def bar(): print('from bar')def foo(func): #func=<function bar at 0x00000225AF631E18> return func #return <function bar at 0x00000225AF631E18># print(bar)f=foo(bar) #f=<function bar at 0x00000225AF631E18># print(f)f()
###以上輸出:
from bar
(4)可以當做容器類型的元素
x=1l=[x,]print(l)def get(): print('from get')def put(): print('from put')l=[get,put]# print(l)l[0]()
###以上輸出:
[1]
from get
注意:
1、 func可以被引用
f=func
2、func可以當做參數傳給x
3、func還可以當做傳回值
4、可以當做容器中類型的元素
函數查詢登入功能的引用:
def auth(): print('請登入:')def reigster(): print('註冊:')def search(): print('查看:')def transfer(): print('轉賬')def pay(): print('支付')dic={ '1':auth, '2':reigster, '3':search, '4':transfer, '5':pay}def interactive(): while True: print(''' 1 認證 2 註冊 3 查看 4 轉賬 5 支付 ''' ) choice = input('請輸入編號:').strip() if choice in dic: dic[choice]() else: print('非法操作')interactive()
五、閉包函數
閉:指的是定義在函數內部的函數
範圍關係,在函數定義階段就規定死了,與調用位置無關
def outter(): x=2 def inner(): # x=1 print('from inner',x) return innerf=outter() #f=innerdef foo(): x=1111111111111111111111111111 f()foo()
###以上輸出:
from inner 2
1、閉包函數:
(1)定義在函數內部的函數
(2)並且該函數包含對外部函數範圍中名字的引用,該函數就稱為閉包函數
瞭解:
為函數體傳值的方式
方式一:將值以參數的形式的傳入
利用爬蟲擷取網站的原始碼:
import requests:def get(url): response=requests.get(url) if response.status_code == 200: print(response.text)get('https://www.baidu.com')
方式二
import requestsimport timedef outter(url): #url='https://www.baidu.com' # url='https://www.baidu.com' def get(): response=requests.get(url) if response.status_code == 200: print(response.text) return getbaidu=outter('https://www.baidu.com')python=outter('https://www.python.org')baidu()print('=====================>')time.sleep(3)baidu()