函數(shù)

函數(shù)

可接受任意數(shù)量參數(shù)的函數(shù)

  • 可以使用 * 使函數(shù)接收任意數(shù)量的位置參數(shù), * 只能是函數(shù)的最后一個位置參數(shù)
  • 可以使用 ** 使函數(shù)接收任意數(shù)量的關(guān)鍵字參數(shù) ** 只能是函數(shù)的最后一個參數(shù)
def func(x, *args, **kwargs):
    print (x)
    print (args)
    print (kwargs)

if __name__ == '__main__':
    func(1, 2, 3, four=4)
out:
1                     
(2, 3)                
{'four': 4}  

只接受關(guān)鍵字參數(shù)的函數(shù)

  • 提升可讀性
def recv(num, block=None):
    pass

msg = recv(1024, block=False)

定義有默認(rèn)參數(shù)的函數(shù)

  • 使用_no_value賦值給object()測試某個參數(shù)是否傳遞了進(jìn)來
  • 傳遞None 和 不傳遞參數(shù)是有差別的
_no_value = object()

def spam(a, b=_no_value):
    if b is _no_value:
        print ('No b value supplied')
    print (a)

if __name__ == '__main__':
    spam(1)
    spam(1, 2)
    spam(1, None)
out:
No b value supplied   
1                     
1                     
1    
  • 默認(rèn)參數(shù)的值應(yīng)該是不可變的對象(None, True, Flase,數(shù)字或字符串)
  • 使用可變對象傳值,可能會無意間修改傳遞的默認(rèn)值
ERROR!
def spam(a, b=[]):
    print (b)
    return b
out:
>>> x = spam(1)
[]
>>> x.append(99)
>>> x.append('Yow!')
>>> x
[99, 'Yow!']
>>> spam(1)
[99, 'Yow!']
  • 在檢查None的時候使用is 操作符否則0, '',都會被檢測為None
def spam(a, b=None):
    #Error
    if not b:
        pass
    #correct
    if b is None:
        pass

定義匿名或內(nèi)聯(lián)函數(shù)(lambda)

  • lambda 只能指定單個表達(dá)式,他的值就是最后的返回值
>>> add = lambda x, y:x+y
>>> add(2, 3)
5
>>> add('hell', 'o')
'hello'

#根據(jù)名的來進(jìn)行排序
>>> names = ['David Beazley', 'Brian Jones','Raymond Hettinger', 'Ned Batchelder']
>>> sorted(names, key=lambda name:name.split()[-1].lower())
['Ned Batchelder', 'David Beazley', 'Raymond Hettinger', 'Brian Jones']

匿名函數(shù)捕獲變量值

  • lambda定義一個匿名函數(shù),定義時捕獲到某些變量值
  • lambda中的x是一個自由變量,在運行時綁定
>>> x = 10
>>> a = lambda y:x+y
>>> x = 20
>>> a(10)
30
  • 在lambda中綁定默認(rèn)值 main(),funcs函數(shù)列表的n值都不同
def main():
    #根據(jù)lambda 生成函數(shù)
    funcs = [lambda x, n=n:x+n for n in range(5)]
    for f in funcs:
        print (f(0))

def main1():
    funcs = [lambda x :x+n for n in range(5)]
    for f in funcs:
        print (f(0))

out:
0,1,2,3,4

減少函數(shù)的參數(shù)個數(shù)

  • 使用functools.partial()減少函數(shù)的調(diào)用參數(shù)值
  • partial會返回一個新的函數(shù)對象,新的函數(shù)會合并已經(jīng)賦值的參數(shù),最后傳遞給原始函數(shù)
def spam(a, b, c, d):
    print (a, b, c, d)
>>> s1 = partial(spam, d=42)
>>> s1(1, 2, 3)
1 2 3 42

將單方法的類轉(zhuǎn)換為函數(shù)

  • class 中除了init()外,只定義了一個方法
  • 使用類的原因是需要存儲一些額外的值給template調(diào)用,比如本例中的template
  • 在閉包中,一個閉包就是一個函數(shù),閉包會記住自己在定義時的環(huán)境,在本例中opener()函數(shù)記住了template變量
  • 給函數(shù)使用閉包比使用類更加簡潔
from urllib.request import urlopen
class UrlTemplate:
    def __init__(self, template):
        self.template = template

    def open(self, **kwargs):
        return urlopen(self.template.format_map(kwargs))

def urltemplate(template):
    def opener(**kwargs):
        return urlopen(template.format_map(kwargs))
    return opener

if __name__ == '__main__':
    yahoo = UrlTemplate('http://finance.yahoo.com/d/quotes.csv?s={names}&f={fields}')
    for line in yahoo.open(names='IBM,AAPL,FB', fields='sl1c1v'):
        print (line.decode('utf-8'))


    yahoo = urltemplate('http://finance.yahoo.com/d/quotes.csv?s={names}&f={fields}')
    for line in yahoo(names='IBM,AAPL,FB', fields='sl1c1v'):
        print (line.decode('utf-8'))

帶額外狀態(tài)信息的回調(diào)函數(shù)

  • 為了讓回調(diào)函數(shù)訪問外部信息,可以給回調(diào)函數(shù)綁定一個額外的方法,通過訪問類的所屬變量來達(dá)到目的
def apply_async(func, args, *, callback):
    result = func(*args)
    callback(result)

def print_result(result):
    print ('Got:', result)

def add(x, y):
    return x + y

class ResultHandler:
    def __init__(self):
        self.sequence = 0

    def handler(self, result):
        self.sequence += 1
        print ('[{}] Got:{}'.format(self.sequence, result))

if __name__ == '__main__':
    apply_async(add, (2, 3), callback=print_result)
    #通過類方法來訪問外部信息
    r = ResultHandler()
    apply_async(add, (2, 3), callback=r.handler)
    apply_async(add, ('hello', 'world'), callback=r.handler)
  • 可以使用閉包來代替類方法來捕獲狀態(tài)值
  • nonlocal 聲明可以改變閉包中的參數(shù)值,這里是sequence += 1
def apply_async(func, args, *, callback):
    result = func(*args)
    callback(result)

def add(x, y):
    return x + y

def make_handler():
    sequence = 0
    def handler(result):
        nonlocal sequence
        sequence += 1
        print('[{}] Got: {}'.format(sequence, result))
    return handler

if __name__ == '__main__':
    #使用閉包捕獲狀態(tài)值
    handler = make_handler()
    apply_async(add, (2, 3), callback=handler)
    apply_async(add, ('hello', 'world'), callback=handler)

訪問閉包中定義的變量,并且可以修改

  • 閉包的內(nèi)部變量對于外界是完全隱藏的,但可以編寫訪問函數(shù)并將函數(shù)綁定到閉包上實現(xiàn)
  • 函數(shù)屬性允許我們用一種很簡單的方式將訪問方法綁定到閉包函數(shù)上,這個跟實例方法很像
def sample():
    n = 0
    def func():
        print ('n=', n)

    def get_n():
        return n

    def set_n(value):
        nonlocal n
        n = value

    func.get_n = get_n
    func.set_n = set_n
    return func
out:
>>> f = sample()
>>> f()
n= 0
>>> f.set_n(10)
>>> f()
n= 10
>>> f.get_n()
10
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容