本文實例講述了Python函數基礎用法。分享給大家供大家參考,具體如下:
一、什么是命名關鍵字參數?
格式: 在*后面參數都是命名關鍵字參數。
特點:
1、約束函數的調用者必須按照Kye=value的形式傳值。
2、約束函數的調用者必須用我們指定的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'>
二、函數的嵌套
1、函數的嵌套調用:在函數內又調用了其他函數
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、函數的嵌套定義:在函數內又定義其他函數。
1
2
3
4
5
6
7
8
9
|
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、名稱空間:存放名字與值綁定關系的地方
1
2
3
|
x = 888888888 def func(): pass |
2、名稱空間分為三類
(1)內置名稱空間:存放python解釋器自帶的名字,在解釋器啟動時就生效,解釋器關閉則失效。
(2)全局名稱空間:文件級別的名字,在執行文件的時候生效,在文件結束或者在文件執行期間被刪除則失效。
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)局部名稱空間:存放函數自定義的名字(函數的參數以及函數內的名字都存放與局部名稱空間),在函數調用時臨時生效,函數結束則失效。
注意:
加載順序:內置名稱空間-------->>全局名稱空間------->>>局部名稱空間
查找名字:局部名稱空間-------->>全局名稱空間------->>>內置名稱空間
1
2
3
4
5
6
7
|
def f1(): # len=1 def f2(): # len=2 print ( len ) f2() f1() |
以上輸出:
global
3、作用域
全局作用域:包涵的是內置名稱空間與全局名稱空間的名字。
特點:
- (1)在任何位置都能夠訪問的到
- (2)該范圍內的名字會伴隨程序整個生命周期
局部作用域:包含的是局部名稱空間的名字
特點:
- (1)只在函數內使用
- (2)調用函數時生效,調用結束失效
四、函數對象
1、函數在python中是第一類對象
(1)可以被引用
1
2
3
4
5
6
|
x = 1 y = x def bar(): print ( 'from bar' ) f = bar f() |
以上輸出:
from bar
(2)可以當做參數傳入
1
2
3
4
|
x = 1 def func(a): print (a) func(x) |
以上輸出:
1
(3)可以當做函數的返回值
代碼(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可以當做參數傳給x
3、func還可以當做返回值
4、可以當做容器中類型的元素
函數查詢登錄功能的引用:
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 ( '轉賬' ) 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() |
五、閉包函數
閉:指的是定義在函數內部的函數
作用域關系,在函數定義階段就規定死了,與調用位置無關
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、閉包函數:
(1)定義在函數內部的函數
(2)并且該函數包含對外部函數作用域中名字的引用,該函數就稱為閉包函數
了解:
為函數體傳值的方式
方式一:將值以參數的形式的傳入
利用爬蟲獲取網站的源代碼:
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程序設計有所幫助。
原文鏈接:https://www.cnblogs.com/lyox/p/8665386.html