More Information

more_return_values.py

# 讓一個(gè)函數(shù)返回兩個(gè)或多個(gè)不同的值

def get_morevalues():
    shoplist = ['apple','mango','carrot']
    str = 'This is the second value'
    a = 3
    # 通過一個(gè)元組就可以返回多個(gè)值
    return(shoplist,str,a)

# Unpackage 要注意數(shù)量要對(duì)應(yīng)

first_value,second_value,third_value = get_morevalues()

print(first_value)
print(second_value)
print(third_value)

more_swaptip.py

# Python中交換兩個(gè)變量的方法
# a,b = some expression 會(huì)把表達(dá)式的結(jié)果解釋為具有兩個(gè)值的一個(gè)元組
a = 5
b = 8
print('a = {}, b = {}'.format(a,b))
a,b = b,a
print('a = {}, b = {}'.format(a,b))

more_single_sentenceblock.py

# 單個(gè)語句塊
# 如果語句塊只包括單獨(dú)的一句語句 那么你可以在同一行指定它
# 當(dāng)然這種方法不建議使用
flag = True
if flag: print('Yes')

more_lambda.py

# lambda語句
# 它可以創(chuàng)建一個(gè)新的函數(shù)對(duì)象 
# 需要一個(gè)參數(shù),后跟一個(gè)表達(dá)式做為函數(shù)體,表達(dá)式的值作為函數(shù)的返回值

points = [{'x': 2, 'y': 3}, {'x': 4, 'y': 1}]

# 還不是很懂這句話
points.sort(key=lambda i:i['y'])

print(points)

more_list_comprehension.py

# 用現(xiàn)有的列表中得到一份新列表

listone = [2,3,4]
listtwo = [2*i for i in listone if i > 2]
print(listone)  # listone不變
print(listtwo)

more_receive_tuple_and_dict.py

# 在函數(shù)中接受元組與字典
# 用 * 作為元組前綴
# 用 ** 作為字典前綴

def powersum(power, *args):
    '''Return the sum of each argument raised to the specified power.'''
    total = 0
    for i in args:
        total += pow(i,power)
    return total

print(powersum(2,3,4,5)) 
print(powersum(2,10))

more_assert.py

# Assert語句
# assert用來斷言某事是真
# ex:你非常確定你正在使用的列表中至少包含一個(gè)元素,想確認(rèn)這一點(diǎn),
# 如果其不是真的就跑出一個(gè)錯(cuò)誤

mylist = ['item']
assert len(mylist) >= 1
print(mylist.pop())

assert len(mylist) >= 1

more_decorates.py

# 關(guān)于裝飾器一點(diǎn)都看不懂 到時(shí)候看Cookbook吧
# 裝飾器Decorators 能夠包裝函數(shù)
from time import sleep
from functools import wraps
import logging

logging.basicConfig()
log = logging.getLogger("retry")

def retry(f):
    @wraps(f)
    def wrapped_f(*args,**kwargs):
        MAX_ATTEMPTS = 5
        for attempt in range(1,MAX_ATTEMPTS+1):
            try:
                return f(*args,**kwargs)
            except:
                log.exception("Attemp %s%s failed : %s",attempt,MAX_ATTEMPTS,(args,kwargs))
                sleep(10 * attempt)
        log.critial("All %s attempts failed : %s",MAX_ATTEMPTS,(args,kwargs))
    return wrapped_f
    
counter = 0
    
@retry
def save_to_database(arg):
    print('Write to a database or make a network call or etc.')
    print('This will be automatically retired if exception is thrown.')
    global counter
    count += 1
        
    if counter >= 2:
        raise ValueError(arg)

if __name__ == '__main__':
    save_to_database('Some bad value')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 個(gè)人筆記,方便自己查閱使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik閱讀 67,947評(píng)論 0 5
  • The Python Data Model If you learned another object-orien...
    plutoese閱讀 1,954評(píng)論 0 51
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • The programs we have looked at thus far have dealt with t...
    誦成讀書閱讀 887評(píng)論 0 0
  • 早晨的餐桌經(jīng)常會(huì)出現(xiàn)的就是白煮蛋,既有營養(yǎng)又快捷方便??墒侨绾窝杆賱兂鲆幻豆饣陌纂u蛋呢?請跟著我的口令一起來! ...
    慢享生活閱讀 696評(píng)論 2 3

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