|
函數(shù)在BASIC中叫做subroutine(子過程或子程序),在Pascal中叫做procedure(過程)和function,在C中只有function,在Java里面叫做method。
定義: 函數(shù)是指將一組語句的集合通過一個(gè)名字(函數(shù)名)封裝起來,要想執(zhí)行這個(gè)函數(shù),只需調(diào)用其函數(shù)名即可
特性: 1. 減少重復(fù)代碼 2.使程序變的可擴(kuò)展 3.使程序變得易維護(hù)
形參: 形式參數(shù),不是實(shí)際存在,是虛擬變量。在定義函數(shù)和函數(shù)體的時(shí)候使用形參,目的是在函數(shù)調(diào)用時(shí)接收實(shí)參(實(shí)參個(gè)數(shù),類型應(yīng)與實(shí)參一一對(duì)應(yīng))
實(shí)參: 實(shí)際參數(shù),調(diào)用函數(shù)時(shí)傳給函數(shù)的參數(shù),可以是常量,變量,表達(dá)式,函數(shù),傳給形參
參數(shù): 1.必備參數(shù) 2.關(guān)鍵字參數(shù) 3.默認(rèn)參數(shù)(缺省參數(shù)) 4.不定長參數(shù):加了星號(hào)(*)的變量名會(huì)存放所有未命名的變量參數(shù)。而加(**)的變量名會(huì)存放命名的變量參數(shù) (*args) (**kargs)
返回值return
高階函數(shù)是至少滿足下列一個(gè)條件的函數(shù):1.接受一個(gè)或多個(gè)函數(shù)作為輸入 2.輸出一個(gè)函數(shù)
作用域: 搜索變量的優(yōu)先級(jí)順序依次是:LEGB,作用域局部>外層作用域>當(dāng)前模塊中的全局>python內(nèi)置作用域。
L: local,局部作用域,即函數(shù)中定義的變量;
E: enclosing,嵌套的父級(jí)函數(shù)的局部作用域,即包含此函數(shù)的上級(jí)函數(shù)的局部作用域,但不是全局的;
G: globa,全局變量,就是模塊級(jí)別定義的變量;
B: built-in,系統(tǒng)固定模塊里面的變量,比如int, bytearray等。
在Python中,只有模塊(module),類(class)以及函數(shù)(def、lambda)才會(huì)引入新的作用域,當(dāng)內(nèi)部作用域想修改外部作用域的變量時(shí),就要用到global(改全局變量)和nonlocal(改嵌套變量)
def func(n): #在函數(shù)內(nèi)部,可以調(diào)用其他函數(shù)。如果一個(gè)函數(shù)在內(nèi)部調(diào)用自身本身,這個(gè)函數(shù)就是遞歸函數(shù)。
if n==1:
return 1
return n*func(n-1)
def fibo(n): #斐波那契 0 1 1 2 3 5 8 13 21 ……
if n==0 or n==1:
return n
return fibo(n-1) fibo(n-2)
# 重要的內(nèi)置函數(shù):
# 1.filter(function, sequence)
str = ['a', 'b', 'c', 'd']
def fun1(s):
if s != 'a':
return s
ret = filter(fun1, str)
print(list(ret)) # ret是一個(gè)迭代器對(duì)象['b', 'c', 'd']
# 對(duì)sequence中的item依次執(zhí)行function(item),將執(zhí)行結(jié)果為True的item做成一個(gè)filterobject的迭代器返回。可以看作是過濾函數(shù)。
# 2.map(function, sequence)
str = ['a', 'b']
def fun2(s):
return s "alvin"
ret = map(fun2, str)
print(ret) # map object的迭代器:<map object at 0x00000229E73D3898>
print(list(ret)) # ['aalvin', 'balvin']
# 對(duì)sequence中的item依次執(zhí)行function(item),將執(zhí)行結(jié)果組成一個(gè)mapobject迭代器返回.
# map也支持多個(gè)sequence,這就要求function也支持相應(yīng)數(shù)量的參數(shù)輸入:
def add(x, y):
return x y
print(list(map(add, range(10), range(10)))) ##[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 3.reduce(function, sequence, starting_value)
from functools import reduce
def add1(x, y):
return x y
print(reduce(add1, range(1, 101))) ## 5050
print(reduce(add1, range(1, 101), 20)) ## 5070
# 對(duì)sequence中的item順序迭代調(diào)用function,如果有starting_value,還可以作為初始值調(diào)用.
# 4.lambda
# 普通函數(shù)
def add(a, b):
return a b
print(add(2, 3))
# 匿名函數(shù):因?yàn)閘amdba在創(chuàng)建時(shí)不需要命名
add = lambda a, b: a b
print(add(2, 3))
# 匿名函數(shù)的命名規(guī)則,用lamdba 關(guān)鍵字標(biāo)識(shí),冒號(hào)(:)左側(cè)表示函數(shù)接收的參數(shù)(a,b),冒號(hào)(:)右側(cè) 表示函數(shù)的返回值(a b)。
# Built - in Functions:
# abs() dict() help() min() setattr()
# all() dir() hex() next() slice()
# any() divmod() id() object() sorted()
# ascii() enumerate() input() oct() staticmethod()
# bin() eval() int() open() str()
# bool() exec() isinstance() ord() sum()
# bytearray() filter() issubclass() pow() super()
# bytes() float() iter() print() tuple()
# callable() format() len() property() type()
# chr() frozenset() list() range() vars()
# classmethod() getattr() locals() repr() zip()
# compile() globals() map() reversed() __import__()
# complex() hasattr() max() round()
# delattr() hash() memoryview() set()
來源:https://www./content-4-333851.html
|