您的位置:首页 > 编程语言 > Python开发

Python学习笔记12

2016-04-25 21:35 597 查看

chapter_12

文档

注释

Python 用’#’号添加注释.

Python的注释会保存到__doc__属性中

#docstrings.py

"""
Module documentation
Words Go Here
"""

spam = 40

def square(x):
"""
function documentation
can we have your liver then?
"""

return x**2     #square

class Employee:
"class documentation"
pass

print(square(4))
print(square.__doc__)


>>> import docstrings
16

function documentation
can we have your liver then?

>>> print(docstrings.__doc__) #显示文件注释

Module documentation
Words Go Here

>>> print(docstrings.square.__doc__) #显示函数注释

function documentation
can we have your liver then?

>>> print(docstrings.Employee.__doc__) #显示类注释
class documentation


常见编写代码的陷阱

别忘了冒号:一定要记住在复合语句首行末尾输入’:’

从第一列开始,要确保顶层程序代码从第一列开始

缩进要一致

不要在Python中写C代码

使用简单的for循环,而不是while或range

要注意赋值语句中的可变对象,防止共享引用

不要期待在原处修改对象的函数会返回结果,如list.append,list.sort

一定要使用括号调用函数

不要在导入和重载中使用扩展名和路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: