Python函數(shù)基礎(chǔ)實例詳解【函數(shù)嵌套,命名空間,函數(shù)對象,閉包函數(shù)等】
本文實例講述了Python函數(shù)基礎(chǔ)用法。分享給大家供大家參考,具體如下:
一、什么是命名關(guān)鍵字參數(shù)?
格式: 在*后面參數(shù)都是命名關(guān)鍵字參數(shù)。
特點:
1、約束函數(shù)的調(diào)用者必須按照Kye=value的形式傳值。
2、約束函數(shù)的調(diào)用者必須用我們指定的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'>
二、函數(shù)的嵌套
1、函數(shù)的嵌套調(diào)用:在函數(shù)內(nèi)又調(diào)用了其他函數(shù)
def max2(x,y):
if x > y:
return x
else:
return y
def max3(x,y,z):
res1=max(x,y)
res2=max(res1,z)
return res2
print(max3(88,99,100))
以上輸出:
100
2、函數(shù)的嵌套定義:在函數(shù)內(nèi)又定義其他函數(shù)。
def func1():
print('from func1')
def func2(): #func2=內(nèi)存地址
print('from func2')
print(func2) #<function func1.<locals>.func2 at 0x0000024907A098C8>
func2()
func2()
func2()
func1()
以上輸出:
from func2
from func2
from func2
三、函數(shù)的名稱空間
1、名稱空間:存放名字與值綁定關(guān)系的地方
x=888888888 def func(): pass
2、名稱空間分為三類
(1)內(nèi)置名稱空間:存放python解釋器自帶的名字,在解釋器啟動時就生效,解釋器關(guān)閉則失效。
(2)全局名稱空間:文件級別的名字,在執(zhí)行文件的時候生效,在文件結(jié)束或者在文件執(zhí)行期間被刪除則失效。
x=1
def f1():
def f2():
print(x)
f2()
f1()
if 10 > 3:
y=33333
while True:
xxxxx=123123123
以上輸出:
1
(3)局部名稱空間:存放函數(shù)自定義的名字(函數(shù)的參數(shù)以及函數(shù)內(nèi)的名字都存放與局部名稱空間),在函數(shù)調(diào)用時臨時生效,函數(shù)結(jié)束則失效。
注意:
加載順序:內(nèi)置名稱空間-------->>全局名稱空間------->>>局部名稱空間
查找名字:局部名稱空間-------->>全局名稱空間------->>>內(nèi)置名稱空間
def f1():
# len=1
def f2():
# len=2
print(len)
f2()
f1()
以上輸出:
global
3、作用域
全局作用域:包涵的是內(nèi)置名稱空間與全局名稱空間的名字。
特點:
- (1)在任何位置都能夠訪問的到
- (2)該范圍內(nèi)的名字會伴隨程序整個生命周期
局部作用域:包含的是局部名稱空間的名字
特點:
- (1)只在函數(shù)內(nèi)使用
- (2)調(diào)用函數(shù)時生效,調(diào)用結(jié)束失效
四、函數(shù)對象
1、函數(shù)在python中是第一類對象
(1)可以被引用
x=1
y=x
def bar():
print('from bar')
f=bar
f()
以上輸出:
from bar
(2)可以當做參數(shù)傳入
x=1 def func(a): print(a) func(x)
以上輸出:
1
(3)可以當做函數(shù)的返回值
代碼(1)
x=1 def foo(): return x res=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=1
l=[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可以當做參數(shù)傳給x
3、func還可以當做返回值
4、可以當做容器中類型的元素
函數(shù)查詢登錄功能的引用:
def auth():
print('請登錄:')
def reigster():
print('注冊:')
def search():
print('查看:')
def transfer():
print('轉(zhuǎn)賬')
def pay():
print('支付')
dic={
'1':auth,
'2':reigster,
'3':search,
'4':transfer,
'5':pay
}
def interactive():
while True:
print('''
1 認證
2 注冊
3 查看
4 轉(zhuǎn)賬
5 支付
'''
)
choice = input('請輸入編號:').strip()
if choice in dic:
dic[choice]()
else:
print('非法操作')
interactive()
五、閉包函數(shù)
閉:指的是定義在函數(shù)內(nèi)部的函數(shù)
作用域關(guān)系,在函數(shù)定義階段就規(guī)定死了,與調(diào)用位置無關(guān)
def outter():
x=2
def inner():
# x=1
print('from inner',x)
return inner
f=outter() #f=inner
def foo():
x=1111111111111111111111111111
f()
foo()
以上輸出:
from inner 2
1、閉包函數(shù):
(1)定義在函數(shù)內(nèi)部的函數(shù)
(2)并且該函數(shù)包含對外部函數(shù)作用域中名字的引用,該函數(shù)就稱為閉包函數(shù)
了解:
為函數(shù)體傳值的方式
方式一:將值以參數(shù)的形式的傳入
利用爬蟲獲取網(wǎng)站的源代碼:
import requests:
def get(url):
response=requests.get(url)
if response.status_code == 200:
print(response.text)
get('https://www.baidu.com')
方式二
import requests
import time
def 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 get
baidu=outter('https://www.baidu.com')
python=outter('https://www.python.org')
baidu()
print('=====================>')
time.sleep(3)
baidu()
關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python多線程編程(八):使用Event實現(xiàn)線程間通信
這篇文章主要介紹了Python多線程編程(八):使用Event實現(xiàn)線程間通信,,需要的朋友可以參考下2015-04-04
np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn)
本文主要介紹了np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)的使用教程
shape函數(shù)是numpy.core.fromnumeric中的函數(shù),它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
手把手教你怎么用Python實現(xiàn)zip文件密碼的破解
之前在家里的老電腦中,發(fā)現(xiàn)一個加密zip壓縮包,由于時隔太久忘記密碼了,依稀記得密碼是6位字母加數(shù)字,網(wǎng)上下載了很多破解密碼的軟件都沒有效果,于是想到自己用Python寫一個暴力破解密碼的腳本,需要的朋友可以參考下2021-05-05

