Setting the docstring to an expression inside def

后端 未结 4 1794
南笙
南笙 2021-01-19 07:36

I would like to set the func_doc (as an expression) within def.

def f():
    \'\'\'My function help\'\'\' #Set the         


        
4条回答
  •  走了就别回头了
    2021-01-19 08:07

    You cannot. The rule is: A string literal as first statement is taken as docstring. If it's an expression, it is not a string literal, and hence ignored.

    You can also explicitly assign to the docstring attribute afterwards if you really need this. I don't see why you'd need it though. Your reasons seem fishy to me:

    • Importing a function (or anything else, really) gives you the very same object, with the same docstring. Wrapping a function is another story, but for what we have functools.wraps.
    • Factoring out parts of a regex into a separate variable doesn't make anything more readable per se. Having the token definition at hand is vital for understanding the action code.

提交回复
热议问题