[python] naming

python 的命名規則...

其實從前我一直很不care這個,不過為了code的可讀性還有交接  所以還是記錄一下好了

1. lowercase_with_underscores : 用於modules 與internal variables(包括function/method variables) [ 除了__init__裡頭的參數例外。 ]

2. MixedCase  :  用於 modules 與 public properties

3.  mixedCaseExceptFirstWord : 用在 public methods 及functions

4. _lowercase_with_leading_undersore : 用在 private functions, methods, properties

5. __lowercase_with_two_leading_underscore : 用在不被 overwritten的private properties

6. CAPS_WITH_UNDERSCORES  : 用在costants

7. gMixedCase : globals (盡量少用.)

一個標準的 py module :

#!/usr/bin/env python
"""Provides NumberList and FrequencyDistribution, classes for statistics.

NumberList holds a sequence of numbers, and defines several statistical
operations (mean, stdev, etc.) FrequencyDistribution holds a mapping from
items (not necessarily numbers) to counts, and defines operations such as
Shannon entropy and frequency normalization.
接著import函式庫(先是built-in, 再是3rd party, 最後才是自己的)
from math import sqrt, log, e
from random import choice, random
from Utils import indices
再來是authorship information
__author__ = "Rob Knight, Gavin Huttley, and Peter Maxwell"
__copyright__ = "Copyright 2007, The Cogent Project"
__credits__ = ["Rob Knight", "Peter Maxwell", "Gavin Huttley",
"Matthew Wakefield"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"

class NumberList(list):
pass #much code deleted
class FrequencyDistribution(dict):
pass #much code deleted

if __name__ == '__main__': #code to execute if called from command-line
pass #do nothing - code deleted
#use this either for a simple example of how to use the module,
#or when the module can meaningfully be called as a script.


Popular posts from this blog

股票評價(Stock Valuation) - 股利折現模型

openwrt feed的使用

How to convert Markdown into HTML