docstring

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

社会主义新天地 提交于 2019-12-28 11:43:11
问题 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. 回答1: def my_func(): """Docstring

How to put a variable into Python docstring

 ̄綄美尐妖づ 提交于 2019-12-28 05:06:10
问题 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

I've been using this docstring syntax for 2 years now, what's it called, who specifies it, and is it “ok” to use it?

天大地大妈咪最大 提交于 2019-12-23 22:06:45
问题 I've been using this weird syntax for a long time now, and am getting quite good at it. It's like taking the good part about java, and having that in Python. It's awesome, I like it, my IDE even caught some bugs because of the annotations. I don't know however who specified this syntax, how official it is, and I don't know the exact rules for it. Admittedly, I'm using PyCharm, but I don't know if this would work in other IDEs, or if it's just PyCharm syntax - I don'w want to think it is, and

Is there a way to describe/type-hint the contents of a function's parameters?

半腔热情 提交于 2019-12-23 15:41:12
问题 I'm trying to learn how to better document my code. Describing a function and just hinting that it receives dict seems to leave any future reader rather short on information though. Is it common at all to do the following? Or is there maybe another way I've missed reading on the subject? def add_control(self, ctrl_data: dict): """ :param ctrl_data: - name: str - channel: int - control_channel_id: int - default_position: int :type ctrl_data: dict """ Edit: Please actually read a bit into the

Python docstrings templated

有些话、适合烂在心里 提交于 2019-12-23 12:36:17
问题 Why doesn't dynamically formatting docstrings work? Is there an acceptable workaround for doing this at function definition time ? >>> DEFAULT_BAR = "moe's tavern" >>> def foo(bar=DEFAULT_BAR): ... """ ... hello this is the docstring ... ... Args: ... bar (str) the bar argument (default: {}) ... """.format(DEFAULT_BAR) ... >>> foo.__doc__ >>> foo.__doc__ is None True I tried with old-skool style %s formatting and that didn't work either. 回答1: Try something like this (props to @user2357112 for

Extracting “extra” docstrings from Python code?

梦想的初衷 提交于 2019-12-22 12:39:08
问题 Python docstrings that immediately follows the declaration of a class or function are placed in the __doc__ attribute. The Question: How does one extract additional "internal" docstrings that occur later on in a function? Update: Such literal statements are elided by the compiler. Could I perhaps get to them (and their line number) via the AST? Why do I ask? I've had a (not fully baked) idea to use such "internal" docstrings to delineate Given/When/Then sections of an Agile Scenario: def test

about python __doc__ docstring

為{幸葍}努か 提交于 2019-12-22 08:34:26
问题 i want to show docstring of my function, but if i use like this @cost_time def func(): "define ...." blabla print func.__doc__ it will not show the docstring,just because i use some meta programming tricky, how can fix this? 回答1: Your wrapped function returned from the cost_time decorator must have the docstring instead of func . Therefore, use functools.wraps which correctly sets __name__ and __doc__ : from functools import wraps def cost_time(fn): @wraps(fn) def wrapper(): return fn()

Replacing python docstrings [closed]

假如想象 提交于 2019-12-21 04:57:24
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I have written a epytext to reST markup converter, and now I want to convert all the docstrings in my entire library from epytext to reST format. Is there a smart way to read the all the docstrings in a module and write back the replacements? ps: ast module perhaps? 回答1: Pyment

How do I document :rtype: for a function that returns multiple possible data types? [duplicate]

泄露秘密 提交于 2019-12-20 19:47:06
问题 This question already has answers here : How to specify multiple return types using type-hints (3 answers) Closed 10 months ago . In a Python docstring how should one document the :rtype: for a function that can return multiple possible data types? For example, if a function can return defaultdict OR dict OR list , based on the functions parameters, how do you document this? Code example: from collections import defaultdict def read_state(state_file, state_file_type='defaultdict'): ""

Pycharm: Auto generate `:type param:` field in docstring

懵懂的女人 提交于 2019-12-20 18:45:12
问题 When I create a function with parameters, PyCharm offers me to create the docstring with :param param_name: field, which is pretty good. But I also need to add the :type param_name: . So from that : def foo(bar, xyz): return bar + xyz With the generate docstring option i have that (even with Insert 'type' and 'rtype' to the documentation stub enable) : def foo(bar, xyz): """ :param bar: :param xyz: """ return bar + xyz And I would like that : def foo(bar, xyz): """ :param bar: :type bar: