6 個例子教你重構 Python 程式碼
if a:if b:return c
if a and b:return c
if sold > DISCOUNT_AMOUNT:total = sold * DISCOUNT_PRICElabel = f'Total: {total}'else:total = sold * PRICElabel = f'Total: {total}'
if sold > DISCOUNT_AMOUNT:total = sold * DISCOUNT_PRICEelse:total = sold * PRICElabel = f'Total: {total}'
def get_content(entry):for block in entry.get_blocks():yield block
found = Falsefor thing in things:if thing == other_thing:found = Truebreak
found = any(thing == other_thing for thing in things)
x = []x = ['first', 'second']
$ python3 -m timeit "x = list()"5000000 loops, best of 5: 63.3 nsec per loop
$ python3 -m timeit "x = []"20000000 loops, best of 5: 15.8 nsec per loop
for building in buildings:city = 'London'addresses.append(building.street_address, city)
city = 'London'for building in buildings:addresses.append(building.street_address, city)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30239065/viewspace-2739660/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 3個例子教你重構 Python 程式碼Python
- 優化重構Python程式碼6個小建議優化Python
- 一個利用正規表示式進行程式碼重構,去除冗餘程式碼的例子行程
- 程式碼重構:類重構的 8 個小技巧
- 6 個重構方法可幫你提升 80% 的程式碼質量
- 程式碼重構:函式重構的 7 個小技巧函式
- 6 個重構方法可幫你提升 80% 的程式碼質量(轉)
- 程式碼重構--大話重構
- 程式碼重構
- 重構程式碼的7個階段
- 程式碼重構之法——方法重構分析
- 使用JavaScript Function.prototype進行程式碼重構的一些例子JavaScriptFunction行程
- Python練手例子(6)Python
- “硬核”程式碼重構
- 重構 PHP 程式碼PHP
- PHP程式碼重構PHP
- 程式碼重構(四)
- 重構遺留程式碼(6):進攻複雜的方法
- 我的第一個python web開發框架(33)——介面程式碼重構PythonWeb框架
- 6個小技巧教你編寫乾淨整潔的程式碼
- 程式碼重構與單元測試——重構6:使用“多型”取代條件表示式(九)多型
- 程式碼重構技巧(二)
- 談談程式碼重構
- 【讀程式碼重構有感】
- 重構:程式碼異味
- 30個python教你學會優雅的寫程式碼Python
- .NET重構—單元測試的程式碼重構
- Lambda和清潔程式碼的一個重構案例 - frankel
- 一個菜鳥在程式碼重構中的感想
- 10 個例子教你學會 ncat (nc) 命令
- Flutter 6 個建議改善你的程式碼結構Flutter
- 程式碼重構那些事兒
- 重構你的javascript程式碼JavaScript
- 重構 - 程式碼優化技巧優化
- 日記9(程式碼重構)
- 重構 - 程式碼整潔之道
- 重構之提煉程式碼
- 重構改善既有的程式碼設計(重構原則)