第十五章 文档

#1.
#A:dir()抓取对象内可用所有属性, help()会输出传入函数的相关信息
L0 = dir(list)                  #L0 = ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', ...]
help(L0.reverse)                #输出有关reverse函数的信息

#2.
#A:文档字符串:将注释写成字符串,放在模块文件、函数、类语句的顶端,就在任何可执行程序代码之前(#注释在其之前也可以),python会自动封装这个字符串,使其成为__doc__属性
"""
Test.py内容:
#Test
'''
ABC
123
'''

def fun() :
    '''
    Fun
    '''
    pass
"""
import Test;
str = Test.__doc__              #str = '
ABC
123
'
str = Test.fun.__doc__          #str = '
    Fun
    '
str = int.__doc__
"""
str = int(x=0) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
"""
help(int)                       #输出类似于int.__doc__

#3.
#A:不要将if和while的测试条件加上(),虽然这不是错的,但是这并不是必须的
#B:使用简单的for循环而不是range和while
#C:不要在导入中使用扩展名和路径,模块可能以.py也可能以.pyc或者其他来作为后缀名

  

原文地址:https://www.cnblogs.com/szn409/p/6641375.html