本文實例講述了Python函數(shù)基礎(chǔ)用法。分享給大家供大家參考,具體如下:
一、什么是命名關(guān)鍵字參數(shù)?
格式: 在*后面參數(shù)都是命名關(guān)鍵字參數(shù)。
特點:
1、約束函數(shù)的調(diào)用者必須按照Kye=value的形式傳值。
2、約束函數(shù)的調(diào)用者必須用我們指定的Key名。
1
2
3
4
5
6
|
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ù)
1
2
3
4
5
6
7
8
9
10
|
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ù)。
1
2
3
4
5
6
7
8
9
|
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)系的地方
1
2
3
|
x = 888888888 def func(): pass |
2、名稱空間分為三類
(1)內(nèi)置名稱空間:存放python解釋器自帶的名字,在解釋器啟動時就生效,解釋器關(guān)閉則失效。
(2)全局名稱空間:文件級別的名字,在執(zhí)行文件的時候生效,在文件結(jié)束或者在文件執(zhí)行期間被刪除則失效。
1
2
3
4
5
6
7
8
9
10
|
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)置名稱空間
1
2
3
4
5
6
7
|
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)可以被引用
1
2
3
4
5
6
|
x = 1 y = x def bar(): print ( 'from bar' ) f = bar f() |
以上輸出:
from bar
(2)可以當做參數(shù)傳入
1
2
3
4
|
x = 1 def func(a): print (a) func(x) |
以上輸出:
1
(3)可以當做函數(shù)的返回值
代碼(1)
1
2
3
4
5
|
x = 1 def foo(): return x res = foo() print (res) |
以上輸出:
1
代碼(2)
1
2
3
4
5
6
7
8
|
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)可以當做容器類型的元素
1
2
3
4
5
6
7
8
9
10
|
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可以被引用
1
|
f = func |
2、func可以當做參數(shù)傳給x
3、func還可以當做返回值
4、可以當做容器中類型的元素
函數(shù)查詢登錄功能的引用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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)
1
2
3
4
5
6
7
8
9
10
11
|
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)站的源代碼:
1
2
3
4
5
6
|
import requests: def get(url): response = requests.get(url) if response.status_code = = 200 : print (response.text) get( 'https://www.baidu.com' ) |
方式二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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() |
希望本文所述對大家Python程序設(shè)計有所幫助。
原文鏈接:https://www.cnblogs.com/lyox/p/8665386.html