ch15文档(python)

# #表示注释

#dir内置函数显示对象内所有属性
>>> import sys
>>> dir(sys)


@查看内置类型的属性,这些属性中的双下划线开头和结尾的属性可用于运算符重载
>>> dir([])

>>> dir('')

>>> dir(str) == dir('')           # Same result as prior example

>>> dir(list) == dir([])

################################################################################################################
#文档字符串 __doc__  写成字符串,放在模块文件,函数以及类语句的顶端,使其成为对象的__doc__属性
### file: 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


#查看模块文档
>>> import sys
>>> print(sys.__doc__)


#查看内置模块内的函数,类的文档字符串
>>> print(sys.getrefcount.__doc__)


#查看内置函数的文档字符串
>>> print(int.__doc__)

>>> print(map.__doc__)


#PyDoc用来提取和格式化文档字符串,PyDoc提供2个接口
1:help内置函数
2:PyDoc GUI/HTML接口

>>> import sys
>>> help(sys.getrefcount)

>>> help(sys)

>>> help(dict)

>>> help(str.replace)

>>> help(ord)



>>> import docstrings
>>> help(docstrings.square)
Help on function square in module docstrings:

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

>>> help(docstrings.Employee)
Help on class Employee in module docstrings:

class Employee(builtins.object)
 |  class documentation
 |
 |  Data descriptors defined here:
 ...more omitted...

>>> help(docstrings)
Help on module docstrings:

NAME
    docstrings

FILE
    c:\misc\docstrings.py

DESCRIPTION
    Module documentation
    Words Go Here

CLASSES
    builtins.object
        Employee

    class Employee(builtins.object)
     |  class documentation
     |
     |  Data descriptors defined here:
     ...more omitted...

FUNCTIONS
    square(x)
        function documentation
        can we have your liver then?

DATA
    spam = 40



#PyDoc同样提供了GUI接口:Python34\Toos\Scripts\pydocgui.pyw


#python标准手册

#### lab code


for i in range(50):
    print('hello %d\n\a' % i)


## file: power.py

L = [1, 2, 4, 8, 16, 32, 64]
X = 5

found = False
i = 0
while not found and i < len(L):
    if 2 ** X == L[i]:
        found = True
    else:
        i = i+1

if found:
    print('at index', i)
else:
    print(X, 'not found')

C:\book\tests> python power.py
at index 5




评论

此博客中的热门博文

OAuth 2教程

网格策略

apt-get详细使用