Python的编码习惯

本文探讨一下Python的编码习惯。写python已经有6个年头了,但是发现对于怎么写python代码很随便,所以打算整理一下规则。

函数的注释

关于函数的注释,python的PEP有规定,
注释应写在函数名的下面,用3个双引号开头,然后注释本身,最后以3个双引号结束

def kos_root():
    """Return the pathname of the KOS root directory."""
    global _kos_root
    if _kos_root: return _kos_root
    ...

或者

def kos_root():
    """
        Return the pathname of the KOS root directory.
        """
    global _kos_root
    if _kos_root: return _kos_root
    ...

PEP257

以这种方式添加注释后,可以使用python的标准方法得到一个函数的帮助

help(kos_root)
Return the pathname of the KOS root directory.

Python的文件头部注释

关于这一部分在
PEP-8

给个例子代码

'''
    File name: test.py
    Author: FoxAVideo
    Date created: 4//2017
    Date last modified: 4/25/2017
    Python Version: 2.7
'''

或者

"""
    File name: test.py
    Author: FoxAVideo
    Date created: 4//2017
    Date last modified: 4/25/2017
    Python Version: 2.7
"""

未完待续…

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则
按侵权处理.

    分享到:
This entry was posted in Python. Bookmark the permalink.

2 Responses to Python的编码习惯

  1. 笑八达 says:

    感受学习的力量!

Leave a Reply

Your email address will not be published. Required fields are marked *

*