docstring

How to write meaningful docstrings?

梦想与她 提交于 2019-11-29 00:07:14
问题 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

How to put a variable into Python docstring

半世苍凉 提交于 2019-11-28 17:25:43
So I'm trying to create a "dynamic" docstring which is something like this: ANIMAL_TYPES = ["mammals", "reptiles", "other"] def func(animalType): """ This is a sample function. @param animalType: "It takes one of these animal types %s" % ANIMAL_TYPES """ to basically let the docstring for @param animalType show whatever ANIMAL_TYPES has; so that when this variable is updated, the docstring will be updated automatically. Unfortunately, however, it doesn't seem working... Does anyone know if there is a way of achieving this? Chris Morgan Triple-quoted strings are one big string. Nothing is

How to document Python code with doxygen [closed]

守給你的承諾、 提交于 2019-11-28 15:41:47
I like doxygen to create documentation of C or PHP code. I have an upcoming Python project and I think I remember that Python doesn't have /* .. */ comments, and also has its own self-documentation facility which seems to be the pythonic way to document. Since I'm familiar with doxygen, how can I use it to produce my Python documentation? Is there anything in particular that I need to be aware of? Blair Conrad This is documented on the doxygen website , but to summarize here: You can use doxygen to document your Python code. You can either use the Python documentation string syntax: """

“Expected an indented block” error?

筅森魡賤 提交于 2019-11-28 09:47:24
I can't understand why python gives an "Expected indentation block" error? """ This module prints all the items within a list""" def print_lol(the_list): """ The following for loop iterates over every item in the list and checks whether the list item is another list or not. in case the list item is another list it recalls the function else it prints the ist item""" for each_item in the_list: if isinstance(each_item, list): print_lol(each_item) else: print(each_item) You have to indent the docstring after the function definition there (line 3, 4): def print_lol(the_list): """this doesn't works"

How to print Docstring of python function from inside the function itself?

余生颓废 提交于 2019-11-28 06:08:51
I want to print the docstring of a python function from inside the function itself. for eg. def my_function(self): """Doc string for my function.""" # print the Docstring here. At the moment I am doing this directly after my_function has been defined. print my_function.__doc__ But would rather let the function do this itself. I have tried calling print self.__doc__ print self.my_function.__doc__ and print this.__doc__ inside my_function but this did not work. kindall def my_func(): """Docstring goes here.""" print my_func.__doc__ This will work as long as you don't change the object bound to

print(__doc__) in Python 3 script

拟墨画扇 提交于 2019-11-28 04:51:28
I can't figure out what does the print(__doc__) do at the beginning of a script, like in this Scikit example . I have been looking for Python docstrings in google, and it seems __doc__ is useful to provide some documentation in, say, functions. But I can't see what does __doc__ do in the middle of a script. it seems __doc__ is useful to provide some documentation in, say, functions This is true. In addition to functions, documentation can also be provided in modules. So, if you have a file named mymodule.py like this: """This is the module docstring.""" def f(x): """This is the function

Adding docstrings to namedtuples?

守給你的承諾、 提交于 2019-11-28 04:36:47
Is it possible to add a documentation string to a namedtuple in an easy manner? I tried from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) """ A point in 2D space """ # Yet another test """ A(nother) point in 2D space """ Point2 = namedtuple("Point2", ["x", "y"]) print Point.__doc__ # -> "Point(x, y)" print Point2.__doc__ # -> "Point2(x, y)" but that doesn't cut it. Is it possible to do in some other way? Mark Rushakoff You can achieve this by creating a simple, empty wrapper class around the returned value from namedtuple . Contents of a file I created ( nt.py ): from

How to document class attributes in Python? [closed]

浪尽此生 提交于 2019-11-28 04:14:42
I'm writing a lightweight class whose attributes are intended to be publicly accessible, and only sometimes overridden in specific instantiations. There's no provision in the Python language for creating docstrings for class attributes, or any sort of attributes, for that matter. What is the accepted way, should there be one, to document these attributes? Currently I'm doing this sort of thing: class Albatross(object): """A bird with a flight speed exceeding that of an unladen swallow. Attributes: """ flight_speed = 691 __doc__ += """ flight_speed (691) The maximum speed that such a bird can

python “help” function: printing docstrings

前提是你 提交于 2019-11-27 17:34:57
问题 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? 回答1: To get

How to comment out a block of code in Python [duplicate]

三世轮回 提交于 2019-11-27 16:35:56
This question already has an answer here: Why doesn't Python have multiline comments? 17 answers Is there a mechanism to comment out large blocks of Python code? Right now, the only ways I can see of commenting out code are to either start every line with a # , or to enclose the code in triple quotes: """ . The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation. After reading all comments, the answer seems to be "No". John Feminella Python does not have such a mechanism. Prepend a # to