我們要做python開發,我想python中的代碼風格我們有必要了解一下
這樣對我們自己和他們所編寫的代碼都有好處的。
下面是8點重要代碼風格注意事項:
ONE : Use 4-space indentation, and no tabs.--用4個空格鍵進行代碼縮進,不要使用tab鍵.
4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.
TWO : Wrap lines so that they don’t exceed 79 characters.-- 每一行代碼不要超過79字符.
This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.
THREE : Use blank lines to separate functions and classes, and larger blocks of code inside functions. -- 在函數或者類中使用空白行
FOUR : When possible, put comments on a line of their own.--添加注釋,這個可以幫助你自己和他人閱讀代碼
FIVE : Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).--在編寫代碼的過程中,注意使用空格,這樣使得你的代碼看上去清晰,美觀
SIX : Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument --使用駝峰命名法命名類名;使用'lower_case_with_underscores '這樣的方式命名方法或者函數
SEVEN : Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case. -- 統一使用UTF-8的編碼方式進行編碼
EIGHT : Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.--不要使用非ascii字符標識符