about 4 years ago

BIF 是 Built-in Function 的縮寫,Python 提供許多好用內建函數可以使用,沒事可以多多瀏覽下面的網頁,避免重造輪子。
https://docs.python.org/2/library/functions.html

這裡列出幾個例子

all()

慣用

def is_valid(file_names):
    return all('py' in name for name in file_names)

非慣用

def is_valid(file_names):
    for name in file_names:
        if not 'py' in name:
            return False
    return True

any()

慣用

def is_dangerous(sql):
    actions = ('update', 'delete', 'replace')
    return any(sql.startswith(action) for action in actions)

非慣用

def is_dangerous(sql):
    actions = ('update', 'delete', 'replace')
    for action in actions:
        if sql.startswith(action):
            return True
    return False

sum()

慣用

prices = [100, 300, 50, 600]
total = sum(prices)

非慣用

prices = [100, 300, 50, 600]
total = 0
for price in prices:
    total += price
← Python 慣用語 - 18 Generator Expressions Python 慣用語 - 20 避免覆蓋 BIFs →
 
comments powered by Disqus