pythonをストレスなく使う!(__doc__とhelp)


目的

 pythonをストレスなく使う!
そのためには、少しでも、理解のレベルを上げる必要あり。
なんでも、こだわって、、、、理解を深める。
ここで記載しているのは、
 __doc__ と help に関してです。

これらの、関数とかの使い方を調べる手段は、『pythonをストレスなく使う!
には、重要だと思います。

 __doc__ と help

__doc__とhelpは、大体、同じようなものが出ます。
helpのほうが、クラスが持っているメソッドの情報とか沢山、情報が出る。
ワタシとしては、沢山でるのが鬱陶しいときもあるが、「q」で抜ければいいだけなので、
helpを使うのがいい気がしてきました。

pprintというモジュールを例に示す。例が悪すぎる気もしますが。。。
末尾にpprintのpprintという関数についても示す。

__doc__の例

>>>
>>> pprint.__doc__
"Support to pretty-print lists, tuples, & dictionaries recursively.\n\nVery simple, but useful, especially in debugging data structures.\n\nClasses\n-------\n\nPrettyPrinter()\n    Handle pretty-printing operations onto a stream using a configured\n    set of formatting parameters.\n\nFunctions\n---------\n\npformat()\n    Format a Python object into a pretty-printed representation.\n\npprint()\n    Pretty-print a Python object to a stream [default is sys.stdout].\n\nsaferepr()\n    Generate a 'standard' repr()-like value, but protect against recursive\n    data structures.\n\n"
>>>

↑ print文に入れないと、改行がボロボロです。

>>> print(pprint.__doc__)
Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.stdout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.


>>>

helpの例。
モジュールファイルの場所が示されるのがいいと思いました。

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

NAME
    pprint - Support to pretty-print lists, tuples, & dictionaries recursively.

DESCRIPTION
    Very simple, but useful, especially in debugging data structures.

    Classes
    -------

    PrettyPrinter()
        Handle pretty-printing operations onto a stream using a configured
        set of formatting parameters.

    Functions
    ---------

    pformat()
        Format a Python object into a pretty-printed representation.

    pprint()
        Pretty-print a Python object to a stream [default is sys.stdout].

    saferepr()
        Generate a 'standard' repr()-like value, but protect against recursive
        data structures.

CLASSES
    builtins.object
        PrettyPrinter

    class PrettyPrinter(builtins.object)
     |  PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)
     |
     |  Methods defined here:
     |
     |  __init__(self, indent=1, width=80, depth=None, stream=None, *, compact=False)
     |      Handle pretty printing operations onto a stream using a set of
     |      configured parameters.
     |
     |      indent
     |          Number of spaces to indent for each level of nesting.
     |
     |      width
     |          Attempted maximum number of columns in the output.
     |
     |      depth
     |          The maximum depth to print out nested structures.
     |
     |      stream
     |          The desired output stream.  If omitted (or false), the standard
     |          output stream available at construction will be used.
     |
     |      compact
     |          If true, several items will be combined in one line.
     |
     |  format(self, object, context, maxlevels, level)
     |      Format object for a specific context, returning a string
     |      and flags indicating whether the representation is 'readable'
     |      and whether the object represents a recursive construct.
     |
     |  isreadable(self, object)
     |
     |  isrecursive(self, object)
     |
     |  pformat(self, object)
     |
     |  pprint(self, object)
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)

FUNCTIONS
    isreadable(object)
        Determine if saferepr(object) is readable by eval().

    isrecursive(object)
        Determine if object requires a recursive representation.

    pformat(object, indent=1, width=80, depth=None, *, compact=False)
        Format a Python object into a pretty-printed representation.

    pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False)
        Pretty-print a Python object to a stream [default is sys.stdout].

    saferepr(object)
        Version of repr() which can handle recursive data structures.

DATA
    __all__ = ['pprint', 'pformat', 'isreadable', 'isrecursive', 'saferepr...

FILE
    c:\users\XYZZ0\appdata\local\programs\python\python37\lib\pprint.py


>>>

pprint.pprint(関数)の例。
これだと、表示が一行なので、printに入れなくても見れますね。

>>> pprint.pprint.__doc__
'Pretty-print a Python object to a stream [default is sys.stdout].'

pydoc

コメントで、pydocというのがあると教えて頂きました。
htmlとかでも吐き出せます。
※ただし、個人的には、デザイン(配色)とかが、ちょっと、好みではなかったです(つまらないことが気になるのが。。。欠点です、ワタシは。)
numpyとかも一気にドキュメントがつくられました。。。凄すぎる。

pydocがうまく動かない場合

何か機能提供しているモジュールとかの場合は、そのモジュールがもつヘルプを表示させるのが、無難な気もします。

-h, --help

とかで、だいたい、でるんですかね。
(先ほど、pydocがうまく動作しないモジュールがあったため。当然、pydocが悪いわけではないですが。。。現実的な対処として。。。)

まとめ

__doc__とhelp、どちらでもいいが、helpのほうがいい気がしました。
わずかな差でもシンプルが好きな人は、__doc__がいいかも。

※ただ、ワタシの環境では、help(numpy)とうつと、張り切って何かだそうとして、エラーになって、2回目に成功しました。helpは沢山だそうとするので、注意が必要かも?

※ docは、\_\_doc\_\_(こんなにはいらないかもしれませんが、)「\」を入れてmarkdownを逃げる必要あり。。

関連(本人)

pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)
pythonをストレスなく使う!(Pylintに寄り添う)
pythonをストレスなく使う!(ExpressionとStatement)
英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

コメントなどあれば、お願いします。
勉強します、、、、

残件

残件【1】

以下の記載が上記にあるが、具体的に、それはなんというモジュールで、
どう動作しないのかが、いつも気になる。

(先ほど、pydocがうまく動作しないモジュールがあったため。