函數(shù)定義
定義函數(shù)使用def 關鍵字開頭,后面是函數(shù)名稱和圓括號()和冒號:
def 函數(shù)名 (參數(shù)列表):
函數(shù)體
def <funName>(arg1,arg2...agrN):
<statements>
def printStr(str):
print(str)
printStr("hello world")
定義一個什么都不做的空函數(shù),可以使用pass語句
def doNothing():
pass
doNothing()
關鍵字參數(shù)
關鍵字參數(shù)和函數(shù)的調(diào)用關系密切,函數(shù)調(diào)用的時候必須使用函數(shù)的參數(shù)名稱確定傳入的參數(shù)值一一對應
def userInfo(name, age):
print("我的名字是%s,我今年%d歲" % (name, age))
userInfo("bx",25)
userInfo(name="user",age=23)
userInfo(age=28,name="ssss")
默認參數(shù)
給參數(shù)設置一個默認值,如果調(diào)用函數(shù)的時候沒有給參數(shù)賦值,就會直接使用設置的函數(shù)默認值
def personInfo(name,age="18"):
print("姓名:"+name,"年齡:"+age)
personInfo("bx")
# 姓名:bx 年齡:18
可變參數(shù)*args
接收不定長的參數(shù),參會類型是元組
def personInfo(*args):
print(args)
personInfo("bx",25,"男")
# ('bx', 25, '男')
# {'name': 'bx', 'age': 25, 'gender': '男'}
**kwargs
接收鍵值對數(shù)據(jù)類型的參數(shù)
def personInfo(**kwargs):
print(kwargs)
personInfo(name="bx",age=25,gender="男")
閉包
- 函數(shù)引用
def test1():
print("--- in test1 func----")
調(diào)用函數(shù)
test1()
引用函數(shù)
ret = test1
print(id(ret))
print(id(test1))
通過引用調(diào)用函數(shù)
ret()
運行結果:
--- in test1 func----
140212571149040
140212571149040
--- in test1 func----
- 什么是閉包
#定義一個函數(shù)
def test(number):
在函數(shù)內(nèi)部再定義一個函數(shù),并且這個函數(shù)用到了外邊函數(shù)的變量,那么將這個函數(shù)以及用到的一些變量稱之為閉包
def test_in(number_in):
print("in test_in 函數(shù), number_in is %d"%number_in)
return number+number_in
#其實這里返回的就是閉包的結果
return test_in
#給test函數(shù)賦值,這個20就是給參數(shù)number
ret = test(20)
#注意這里的100其實給參數(shù)number_in
print(ret(100))
#注意這里的200其實給參數(shù)number_in
print(ret(200))
運行結果:
in test_in 函數(shù), number_in is 100
120
in test_in 函數(shù), number_in is 200
220
- 閉包再理解
內(nèi)部函數(shù)對外部函數(shù)作用域里變量的引用(非全局變量),則稱內(nèi)部函數(shù)為閉包。
# closure.py
def counter(start=0):
count=[start]
def incr():
count[0] += 1
return count[0]
return incr
啟動python解釋器
>>>import closeure
>>>c1=closeure.counter(5)
>>>print(c1())
6
>>>print(c1())
7
>>>c2=closeure.counter(100)
>>>print(c2())
101
>>>print(c2())
102
nonlocal訪問外部函數(shù)的局部變量(python3)
def counter(start=0):
def incr():
nonlocal start
start += 1
return start
return incr
c1 = counter(5)
print(c1())
print(c1())
c2 = counter(50)
print(c2())
print(c2())
print(c1())
print(c1())
print(c2())
print(c2())
- 看一個閉包的實際例子:
def line_conf(a, b):
def line(x):
return a*x + b
return line
line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5))
print(line2(5))
這個例子中,函數(shù)line與變量a,b構成閉包。在創(chuàng)建閉包的時候,我們通過line_conf的參數(shù)a,b說明了這兩個變量的取值,這樣,我們就確定了函數(shù)的最終形式(y = x + 1和y = 4x + 5)。我們只需要變換參數(shù)a,b,就可以獲得不同的直線表達函數(shù)。
lambda 表達式
普通函數(shù)表達式
def add(x,y):
return x+y
print(add(1, 2))
lambda 函數(shù)表達式
add = lambda x, y: x + y
print(add(1, 2))
過濾器函數(shù)
filter 兩個參數(shù)第一個參數(shù)可以是函數(shù)表達式,也可以設置成None,如果是一個函數(shù)的話,則將第二個可迭代的數(shù)據(jù)的每一個元素作為函數(shù)的參數(shù)進行運算。返回時true的值,如果第一個參數(shù)None的,則直接返回迭代器中是true的值
ls = filter(None,[1,2,True,False,0])
print(list(ls))
# [1, 2, True]
def evenNum(x):
return False if x%2 else True
ls = filter(evenNum,range(10))
print(list(ls))
# [0, 2, 4, 6, 8]
ls = filter(lambda x:x%2==0,range(10))
print(list(ls))
# [0, 2, 4, 6, 8]