>>> from env_helper import info; info()
页面更新时间: 2021-08-31 20:54:08
运行环境:
    Linux发行版本: Debian GNU/Linux 11 (bullseye)
    操作系统内核: Linux-5.10.0-8-amd64-x86_64-with-glibc2.31
    Python版本: 3.9.2

1.4. 在代码中适当添加注释

Python中有3种形式的代码注释:块注释、行注释以及文档注释(docstring)。 这3种形式的惯用法大概有如下几种:

  1. 使用块或者行注释的时候仅仅注释那些复杂的操作、算法,还有可能别人难以理解的技巧或者不够一目了然的代码。

  2. 注释和代码隔开一定的距离,同时在块注释之后最好多留几行空白再写代码。下面两行代码显然(1)代码的阅读性要好。

>>> x=0
>>> x=x+1          # increace x by 1 (1)
>>> x=x+1# incredse x by 1           (2)

3)给外部可访问的函数和方法(无论是否简单)添加文挡注释。

注释要清楚地描述方法的功能,并对参数、返回值以及可能发生的异常进行说明,使得外部调用它的人员仅仅看docstring就能正确使用。较为复杂的内部方法也需要进行注释。推荐的函数注释如下:

>>> def FuncName(parameterl ,parameter2):
>>>     """Describe what this function does.
>>>         #such as "Find whether the special string is in the queue or not"
>>>         Args :
>>>             parameterl: parameter typer what is this parameter used for,
>>>                     #such as strqueue:stringfstring queue list for search
>>>             parameter2: parameter typer what is this parameter used for. #such as str: string, string to find
>>>         Returns:
>>>             return type, return value,
>>>             #such as boolean,^epcial string found return Truefelse return False
>>>     """
>>>     print('function body')

4)推荐在文件头中包含copyright申明、模块描述等,如有必要,可以考虑加入作者信 息以及变更记录。

>>> """
>>>     Licensed Materials - Property of CorpA
>>>     (C) Copyright A Corp. 1999, 2011 All Rights Reserved
>>>     CopyRight statement and purpose...
>>> ---------------------------------------
>>> File Name : comments.py
>>> Description. : description what the main function of this file
>>>
>>> Author: Author name
>>> Change Activity:
>>>     list the change activity and time and author information.
>>> ---------------------------------------
>>> """
'n    Licensed Materials - Property of CorpA n    (C) Copyright A Corp. 1999, 2011 All Rights Reservedn    CopyRight statement and purpose...n---------------------------------------nFile Name : comments.pynDescription. : description what the main function of this filennAuthor: Author name nChange Activity:n    list the change activity and time and author information.n---------------------------------------n'

有人说,写代码就像写诗,你见过谁在自己写的诗里加注释吗?这种说法受到许多人的追捧,包括一些Python程序员。 但我的看法是,代码跟诗不太一样.需要适当添加注释。 注释直接关系到代码的可读性和可维护性,同时它还能够帮助发现错误的藏身之处。 因为代码是说明你怎么做的,而好的注释能够说清楚你想做什么,它们相辅相成。 但往往有些程序员 并不重视它,原因是多方面的,有人觉得程序的实现才是最重要的,至于注释是一件浪费时间的事情; 还有的人明明知道注释很重要,可是太懒,不愿意写或者随便应付; 也有人重视注释却不得要领,反而使其成为代码的一种累赘。

下面针对以上几个心态举几个实际中常见例子。

1.4.1. 示例一:

代码即注释(不写注释)。没有注释的代码通常会给他人的阅读和理解带来一定困难,即使是自己写的代码,过一段时间再回头阅读可能也需要一定时间才能理解当初的思路。

>>> a=3
>>> n=5
>>> count,sn,tn = 1,0, 0
>>> while count<=n:
>>>     tn+=a
>>>     sn+=tn
>>>     a*=10
>>>     count+=1
>>> print (sn)
37035

1.4.2. 示例二:

注释与代码重复。注释应该是用来解释代码的功能、原因以及想法的,而不是对代码本身的解释。

>>> # coding=utf-8
>>> print ("please input number n")
>>> n=input()
>>> print ("please input number m:")
>>> m=input()
>>> t=int(n)/2              # t是n的一芈
>>> #循环.条件为t*m/n小于n
>>> while(t*int(m)/(int(n)+1) < int(n)):
>>>     t=0.5*int(m)+int(n)/20       # 重新计算 t 值
>>>     print (t)
>>>     break
please input number n
4
please input number m:
5
2.7

1.4.3. 示例三:

利用注释语法快速删除代码。对于不再需要的代码,应该将其删除,而不是将其注释掉。即使你担心以后还会用到,版本控制工具也可以让你轻松找问被删除的代码。

>>> x=2
>>> y=5
>>> z=3
>>> """following code is no longer needed since there is a better way
>>> if x>y:t=x;x=y;y=t
>>> if x>z:t=z;z=x;x=t
>>> if y>z:t=y;y=z;z=t
>>> print "the order from small to big is: %d %d %d" % (x,y,z)
>>> """
>>> order_list=[x,y,z]
>>> order_list.sort()
>>> print(order_list)
[2, 3, 5]

其他比较常见的问题还包括:代码不断更新而注释却没有更新; 注释比代码本身还复杂繁琐;将别处的注释和代码一起拷贝过来,但上下文的变更导致注释与代码不同步; 更有个别人将注释当做自己的娱乐空间从而留下个性特征等,这几种行为都是平时要注意避免的。