写好python的注释文档很容易

下面是一段非常简单的foo.py

#!usr/bin/env python

"""foo.py -- this is a demo"""

class Foo(object):
    """Foo - this is a empty class,to be developed"""

def printdoc(x):
    """bar(x) - to print the parameters 'x' """
    print x

在python中执行如下过程:
>>> import foo

通过__doc__属性访问模块、函数、类的文档
>>> foo.__doc__
'foo.py -- this is my first damo'
AttributeError: type object 'Foo' has no attribute '__doc'
>>> foo.Foo.__doc__
'Foo -- this is empty class,to be devloped'
>>> foo.bar.__doc__
"bar(x)- to print the para 'x'"

也可以通过内置函数help

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

NAME
    foo - foo.py -- this is my first damo

FILE
    /django/foo.py

CLASSES
    __builtin__.object
        Foo

    class Foo(__builtin__.object)
     |  Foo -- this is empty class,to be devloped
     |
     |  Data and other attributes defined here:
     |
     |  __dict__ = <dictproxy object>
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__ = <attribute '__weakref__' of 'Foo' objects>
     |      list of weak references to the object (if defined)

FUNCTIONS
    bar(x)
        bar(x)- to print the para 'x'

同样help也可用于查询很多你想要的东西

posted @ 2013-06-24 15:15  湾仔小新  阅读(5613)  评论(0编辑  收藏  举报