ホームページ  >  記事  >  バックエンド開発  >  Python 学習ノート - カスタム クラスまたは関数のヘルプ ドキュメントの作成、ドキュメント テストの実施

Python 学習ノート - カスタム クラスまたは関数のヘルプ ドキュメントの作成、ドキュメント テストの実施

黄舟
黄舟オリジナル
2017-01-17 14:27:061995ブラウズ

Python では、help("モジュール名") または help(クラス名) を使用して、クラスまたは関数のドキュメントを表示できます。しかし、それらはどのように書かれているのでしょうか?実際、クラスやメソッドの先頭で複数行のコメントを「"」で囲んでいます。これらの内容は Python ではヘルプドキュメントとして認識されます。ヘルプ ドキュメントには主に次の内容が含まれています:


このクラスまたは関数の主な関数


渡される値と出力


いくつかの特殊な状況の説明


ドキュメントテストの内容


上記の内容は個人的な要約ですが、関連する情報は見ていません


例を挙げてみましょう:

class Apple(object):
""" This is an Apple Class"""
def get_color(self):
"""
Get the Color of Apple.
get_color(self) -> str
"""
return "red"

と入力してください

>>> from CallDemo import Apple
>>> help(Apple)
Help on class Apple in module CallDemo:
class Apple(__builtin__.object)
| This is an Apple Class
| 
 | Methods defined here:
| 
 | get_color(self)
| Get the Color of Apple.
| get_color(self) -> str
| 
 | ----------------------------------------------------------------------
| Data descriptors defined here:
| 
 | __dict__
| dictionary for instance variables (if defined)
| 
 | __weakref__
| list of weak references to the object (if defined)

ドキュメントテストにはdoctestを使用してください

ここにいます コメントでは、ドキュメント テストに doctest モジュールを使用することもできます


たとえば、ドキュメント テスト コンテンツを追加すると、次のようになります:

class Apple(object):
"""
This is an Apple Class
Example:
>>> apple = Apple()
>>> apple.get_color()
'red'
>>> apple.set_count(20)
>>> apple.get_count()
400
"""
def get_color(self):
"""
Get the Color of Apple.
get_color(self) -> str
"""
return "red"
def set_count(self, count):
self._count = count
def get_count(self):
return self._count * self._countif __name__ == '__main__':
import doctest

doctest.testmod()

if __name__ == '__main__':
import doctest
doctest.testmod()

と書いたので、上記のドキュメントはエントリーファイルでテストを実行した場合のみ行われますので、実際のアプリケーションではドキュメントテストは行われません


以上はPythonの勉強メモです。 - カスタム クラスまたは関数のヘルプ ドキュメントの作成と実行 ドキュメントのテストの内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。