变量
Python 的变量,是下划线(_)和英文数字的名称。最开始1文字必须是下划线(_)或者字母。
value1 = 123
_value1 = 123
test_value = 123
TEST_VALUE = 123
常量
Python不支持常量类型。习惯性的大写字母和下划线(_)的变量来命名。
PI = 3.14
MAX_BUFFER_SIZE = 1024
文档引用(__doc__)
module的开始,class的开始,函数的开始用三重双引号 """...""" 的注释、作为文档注释 __doc__ 可以参照注释内容。
mymod.py
Python
"""A sample module"""
class MyClass:
"""A sample class"""
def myfunc(self, x, y):
"""A sample function"""
return x + y
mytest.py
Python
import mymod
print mymod.__doc__ #=> A sample module
print mymod.MyClass.__doc__ #=> A sample class
print mymod.MyClass.myfunc.__doc__ #=> A sample function
help()也可以访问到注释。
Linux
$ python
>>> import mymod
>>> help(mymod)
Help on module mymod:
NAME
mymod - A sample module
FILE
/root/mymod.py
CLASSES
MyClass
class MyClass
| A sample class
|
| Methods defined here:
|
| myfunc(self, x, y)
| A sample function