Search code examples
pythondocstring

Function doesn't get the __doc__ attribute


def foo():
    ("abc"
     "def")

def bar():
    ("abc"
    f"def")

The function foo got a docstring but bar's doc is null.

>>> bar.__doc__
>>> foo.__doc__
'abcdef'

Why does the f-string prevent the function from taking a __doc__ attribute?


Solution

  • Docstrings need to be attached to the function at definition time, but an f-string inside the body of the function would normally need to be evaluated later, during the function call, in order to do interpolation. Since figuring out a sensible way for this to work is hard, f-strings simply aren’t eligible to be considered docstrings; only static string literals are.

    https://github.com/python/cpython/issues/72925