寫好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也可用於查詢很多你想要的東西


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM