Docstring for variable

前端 未结 9 2077
予麋鹿
予麋鹿 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条回答
  •  感动是毒
    2021-01-31 14:07

    No, you can only do this for modules, (lambda and "normal") functions and classes, as far as I know. Other objects, even mutable ones inherit the docstrings of their class and raise AttributeError if you try to change that:

    >>> a = {}
    >>> a.__doc__ = "hello"
    Traceback (most recent call last):
      File "", line 1, in 
    AttributeError: 'dict' object attribute '__doc__' is read-only
    

    (Your second example is valid Python, but the string """l""" doesn't do anything. It is generated, evaluated and discarded.)

提交回复
热议问题