docstring

How do I reference a documented Python function parameter using Sphinx markup?

我们两清 提交于 2019-11-30 07:52:09
I'd like to reference a previously-documented function parameter elsewhere in a Python docstring. Consider the following (admittedly completely artificial) example: def foo(bar): """Perform foo action :param bar: The bar parameter """ def nested(): """Some nested function that depends on enclosing scope's bar parameter. I'd like to reference function foo's bar parameter here with a link, is that possible?""" return bar * bar # ... return nested() Is there a simple way to embed a parameter reference using Sphinx markup, or will this happen automagically? (I'm a complete Sphinx newbie. I've been

Triple-double quote v.s. Double quote

时光怂恿深爱的人放手 提交于 2019-11-30 06:02:05
What is the preferred way to write Python doc string? """ or " In the book Dive Into Python , the author provides the following example: def buildConnectionString(params): """Build a connection string from a dictionary of parameters. Returns string.""" In another chapter , the author provides another example: def stripnulls(data): "strip whitespace and nulls" return data.replace("\00", "").strip() Both syntax work. The only difference to me is that """ allows us to write multi-line doc. Is there any difference other than that? From the PEP8 Style Guide : PEP 257 describes good docstring

How can I parse a numpydoc docstring and access components?

一世执手 提交于 2019-11-30 05:46:00
问题 I'd like to parse a numpydoc docstring and access each component programatically. For example: def foobar(a, b): '''Something something Parameters ---------- a : int, default: 5 Does something cool b : str Wow ''' What I'd like to do is: parsed = magic_parser(foobar) parsed.text # Something something parsed.a.text # Does something cool parsed.a.type # int parsed.a.default # 5 I've been searching around and found things like numpydoc and napoleon but I haven't found any good leads for how to

How do I change Emacs's font face for Python docstrings?

做~自己de王妃 提交于 2019-11-30 05:32:56
问题 I'm just starting to learn Python and use Emacs as my editor. Currently, Emacs uses the same color for normal strings (single quotes) and docstrings (triple quotes). I want docstrings to be a different color, so I used the 'Options->Customize Emacs' menu option to change 'font-lock-doc-face' to a new color and saved the changes. However, Emacs continues to keep docstrings the same color as normal strings. Changing the color of normal strings applies the change to docstrings as well. It would

How can I print a Python file's docstring when executing it?

橙三吉。 提交于 2019-11-30 04:09:15
I have a Python script with a docstring. When the parsing of the command-line arguments does not succeed, I want to print the docstring for the user's information. Is there any way to do this? Minimal example #!/usr/bin/env python """ Usage: script.py This describes the script. """ import sys if len(sys.argv) < 2: print("<here comes the docstring>") The docstring is stored in the module's __doc__ global. print(__doc__) By the way, this goes for any module: import sys; print(sys.__doc__) . Docstrings of functions and classes are also in their __doc__ attribute. Here is an alternative that does

How to write meaningful docstrings?

与世无争的帅哥 提交于 2019-11-30 03:25:55
What, in Your opinion is a meaningful docstring? What do You expect to be described there? For example, consider this Python class's __init__ : def __init__(self, name, value, displayName=None, matchingRule="strict"): """ name - field name value - field value displayName - nice display name, if empty will be set to field name matchingRule - I have no idea what this does, set to strict by default """ Do you find this meaningful? Post Your good/bad examples for all to know (and a general answer so it can be accepted). I agree with "Anything that you can't tell from the method's signature". It

What's meaning of these formats in twisted's docstring?

梦想与她 提交于 2019-11-29 18:50:14
问题 In twisted's sourcecode, many docstrings contain formats like this: L{xxx} or C{xxx} or a line begin with an '@', what's their meanings? for example, in twisted/internet/interfaces.py: def registerProducer(producer, streaming): """ Register to receive data from a producer. ... For L{IPullProducer} providers, C{resumeProducing} will be called once each time data is required. ... @type producer: L{IProducer} provider ... @return: C{None} """ L{IPullProducer} , C{resumeProducing} , @type

How do I reference a documented Python function parameter using Sphinx markup?

…衆ロ難τιáo~ 提交于 2019-11-29 10:36:47
问题 I'd like to reference a previously-documented function parameter elsewhere in a Python docstring. Consider the following (admittedly completely artificial) example: def foo(bar): """Perform foo action :param bar: The bar parameter """ def nested(): """Some nested function that depends on enclosing scope's bar parameter. I'd like to reference function foo's bar parameter here with a link, is that possible?""" return bar * bar # ... return nested() Is there a simple way to embed a parameter

python “help” function: printing docstrings

岁酱吖の 提交于 2019-11-29 03:29:18
Is there an option to print the output of help('myfun'). The behaviour I'm seeing is that output is printed to std.out and the script waits for user input (i.e. type 'q' to continue). There must be a setting to set this to just dump docstrings. Alternatively, if I could just dump the docstring PLUS the "def f(args):" line that would be fine too. Searching for "python help function" is comical. :) Maybe I'm missing some nice pydoc page somewhere out there that explains it all? To get exactly the help that's printed by help(str) into the variable strhelp : import pydoc strhelp = pydoc.render_doc

How can I print a Python file's docstring when executing it?

匆匆过客 提交于 2019-11-29 01:20:19
问题 I have a Python script with a docstring. When the parsing of the command-line arguments does not succeed, I want to print the docstring for the user's information. Is there any way to do this? Minimal example #!/usr/bin/env python """ Usage: script.py This describes the script. """ import sys if len(sys.argv) < 2: print("<here comes the docstring>") 回答1: The docstring is stored in the module's __doc__ global. print(__doc__) By the way, this goes for any module: import sys; print(sys.__doc__)