How can I make Python/Sphinx document object attributes only declared in __init__?

后端 未结 2 2026
一生所求
一生所求 2021-01-12 03:03

I have Python classes with object attributes which are only declared as part of running the constructor, like so:

class Foo(object):
    def __init__(self, b         


        
2条回答
  •  一生所求
    2021-01-12 03:30

    I've tried searching for a standard way to ensure that these "dynamically declared" attributes can be found (and preferably docstring'd) by the parser, but no luck so far. Any suggestions?

    They cannot ever be "detected" by any parser.

    Python has setattr. The complete set of attributes is never "detectable", in any sense of the word.

    You absolutely must describe them in the docstring.

    [Unless you want to do a bunch of meta-programming to generate docstrings from stuff you gathered from inspect or something. Even then, your "solution" would be incomplete as soon as you starting using setattr.]

    class Foo(object):
        """
        :ivar basepath:
        :ivar availableruns:
        """
        def __init__(self, base):
    

提交回复
热议问题