[摘要]Python 最佳實踐指南

wklken發表於2015-12-08

文件地址 The Hitchhiker’s Guide to Python!

這份文件

粗粗粗略地過了一遍,大體撈了一些東西出來,大段大段英文太費眼了,回頭細讀在更新進來

濃縮版,20分鐘可大體過完,然後根據自己需要去看詳細的吧

整體內容還是很不錯的,建議細讀英文

PS:文件含有巨量的TODO(沒寫空白著待補充的),不過但從目錄上來看還是很強大滴,相信完善後,會成為一份很牛逼的指南(難度比官方指南高一點點)


第零部分 Getting Started

連結

不解釋,不翻譯,自個看….真的沒啥(每本入門書籍第一章…)


第一部分 Writing Great Code

Structuring Your Project

連結

import 最佳實踐

Very bad

Better

Best

Python中關於OOP的 觀點

Decorators

動態型別(Dynamic typing)

Avoid using the same variable name for different things.

Bad

Good

It is better to use different names even for things that are related, when they have a different type:

可變和不可變型別(Mutable and immutable types)

字串拼接最佳實踐

Bad

Good

Best

join() is not always best
建立新字串和修改原有字串

字串格式化

Code Style

連結

一般概念(General concepts)

明確的程式碼

Bad

Good

每行一個宣告

Bad

Good

函式引數

Avoid the magical wand(這個腫麼翻…)

方言(Idioms)

Idiomatic Python code is often referred to as being Pythonic.

列舉了一些:

Unpacking

忽略接收變數,這裡用的是兩個下劃線,原因
http://docs.python-guide.org/en/latest/writing/style/#create-an-ignored-variable

同一個元素建立一個長度為N的列表

建立一個長度N的巢狀列表

由列表拼接字串

快速查詢

Zen of Python

PEP8

慣例(Conventions)

判斷值是否等於常量

Bad:

Good:

獲取字典元素

Bad:

Good:

快捷列表操作

Bad:

Good:

Bad:

Good:

使用enumerate

讀檔案

Bad:

Good:

超長的行

Bad:

Good:

Reading Great Code

連結

感受下:The number one thing that Python programmers do is read code.

再感受一把:One of the secrets of becoming a great Python programmer is to read, understand, and comprehend excellent code.

幾個推薦閱讀原始碼專案

Howdoi

Flask

Werkzeug

Requests

Tablib

文件(Documentation)

連結

感受一下:Readability is a primary focus for Python developers, in both project and code documentation.

具體還是讀原文吧

專案文件組成

幾種文件工具

程式碼文件建議

Comments clarify code and begin with a hash (#).

In Python, docstrings describe modules, classes, and functions:

註解程式碼塊

Do not use triple-quote strings to comment code.

This is not a good practice, because line-oriented command-line tools such as grep will not be aware that the commented code is inactive.

It is better to add hashes at the proper indentation level for every commented line.

最佳實踐:

測試你的程式碼(Testing Your Code)

連結

測試一些通用原則

單元測試(Unittest)

Python內建模組, 文件

文件測試(Doctest)

非精細case,只驗證主體功能可用

相關工具

py.text
$ pip install pytest

Nose unittest的擴充套件
$ pip install nose

tox
$ pip install tox

Unittest2
$ pip install unittest2

mock
$ pip install mock

Common Gotchas(不懂怎麼翻…╮(╯▽╰)╭ )

一些新手可能疑惑的例子

兩個例子

1.可變預設引數

What You Wrote

What You Might Have Expected to Happen

What Does Happen

What You Should Do Instead

Python預設引數在函式定義處執行一次,而不是每次函式呼叫時執行。

2.Late Binding Closures(又一個,延遲繫結閉包?)

What You Wrote

What You Might Have Expected to Happen

What Does Happen

What You Should Do Instead

Python的閉包是延時繫結

選擇證照(Choosing a License)

連結

開源證照 列表

證照選擇器 入口


第二部分 Scenario Guide

都是介紹性質的,類似工具目錄,而且大部分是空的,目前沒詳細資訊

要了解具體,goole相關關鍵詞吧

具體自己翻吧 位置

目錄:

Network Applications

Http:

Distributed Systems

Web Applications

Context

Frameworks

Web Servers

WSGI Servers

Hosting

Templating

HTML Scraping

Command Line Applications

GUI Applications

Databases

Networking

Systems Administration

Continuous Integration

Speed

C Extensions

Threading

Scientific Applications

Tools

Libraries

Image Manipulation

XML parsing


有需要自取…..

第三部分 Shipping Great Code

http://docs.python-guide.org/en/latest/#shipping-great-code

第四部分 Development Environment

http://docs.python-guide.org/en/latest/#development-environment

第五部分 Additional Notes

http://docs.python-guide.org/en/latest/#additional-notes


The end!

2013-11-25

wklken

打賞支援我寫出更多好文章,謝謝!

打賞作者

打賞支援我寫出更多好文章,謝謝!

任選一種支付方式

[摘要]Python 最佳實踐指南 [摘要]Python 最佳實踐指南

相關文章