- Return values from one place
- Alternatives to checking for equality
- Continuing a long line of code
\ ()
- Unpacking
a, *rest = [1, 2, 3]
a, *middle, c = [1, 2, 3, 4]
a,__,c = [1, 2, 3] (use __ to replace name on position where its value will not need)
- Creating a length-N list of the same thing
- Whether element in an collection
With statement
__enter__() and __exit__()
Testing your code
(Testing Framwork)Logging
The setLevel()
method, just as in logger objects, specifies the lowest severity that will be dispatched to the appropriate destination. Why are there two setLevel()
methods? The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on
- Choosing Licenses
with EXPR as VAR:
BLOCK
(1)執(zhí)行EXPR語(yǔ)句酒觅,獲取上下文管理器(Context Manager)
(2)調(diào)用上下文管理器中的enter方法嘉汰,該方法執(zhí)行一些預(yù)處理工作。
(3)這里的as VAR可以省略檀夹,如果不省略,則將enter方法的返回值賦值給VAR蚂蕴。
(4)執(zhí)行代碼塊BLOCK纫塌,這里的VAR可以當(dāng)做普通變量使用。
(5)最后調(diào)用上下文管理器中的的exit方法桨昙。
(6)exit方法有三個(gè)參數(shù):exc_type, exc_val, exc_tb检号。如果代碼塊BLOCK發(fā)生異常并退出,那么分別對(duì)應(yīng)異常的type蛙酪、value 和 traceback齐苛。否則三個(gè)參數(shù)全為None。
(7)exit方法的返回值可以為True或者False桂塞。如果為True凹蜂,那么表示異常被忽視,相當(dāng)于進(jìn)行了try-except操作阁危;如果為False玛痊,則該異常會(huì)被重新raise。
- Spliting a long line into two shorter lines that make much better sense.
if badargs:
err = 'create_cookie() got unexpected keyword arguments: {}'
raise TypeError(err.format(sth))