Docstring for variable

前端 未结 9 1930
予麋鹿
予麋鹿 2021-01-31 13:35

Is it posible to use docstring for plain variable? For example I have module called t

def f():
    \"\"\"f\"\"\"

l = lambda x: x
\"\"\"l\"\"\"
         


        
9条回答
  •  旧时难觅i
    2021-01-31 14:14

    A lot of answers assume you want it for offline use and points to sphinx or Epydoc.

    But if you want it for runtime use the answer is that is impossible to add an attribute to another attribute. So you can't attach a doctring to variable.

    When you do:

    a = True
    print(a.__doc__)
    

    You'll be getting the docstring for the bool class. In my application I need it for plug-ins. What I do is to use an associated variable/attribute.

    Something like this:

    a = True
    _help_a = "help for a variable"
    

    As this looks ugly what I'm actually using are syntactic macros (take a look a macropy module). The code looks like this:

    with document:
        a = True
        """ help for a variable """
    

    I explain the whole idea here

提交回复
热议问题