Because the f-string ceases to be literal and by definition ceases to be a candidate to be docstring.
Also by the fact that the docstring is generated at compile time, when generated the byte-code that will be interpreted - as the Maniero comment in its reply, This was a design choice; they could make the value be evaluated at runtime, but they saw no reason for it. So, how to guarantee the value of the objects that will be interpolated? In the case of the question, interpolation occurs only with a global variable and could generate the docstring "Author: Anderson Carlos Woss", but what if I do it at some other point in the code __author__ = 'Foo'
, this should change the docstring? This worsens if we consider that we could add the value of a function parameter in the docstring:
def hello(name):
f'Saudação para {name}'
print(f'Hello, {name}')
In this case, what would be the value of hello.__doc__
? One option would be to consider the string unformatted as docstring, but again, this was a design choice. What sense would have a docstring with the value Autor: {__author__}
?
As for not being another string literal, see the difference between the opcodes generated for a string normal and a f-string.
Normal string
>>> print(dis.dis('"Hello"'))
1 0 LOAD_CONST 0 ('Hello')
2 RETURN_VALUE
None
F-String
>>> print(dis.dis('f"Hello {name}"'))
1 0 LOAD_CONST 0 ('Hello ')
2 LOAD_NAME 0 (name)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
None
The extra steps, to format and build the string end cause the f-string is no longer just a literal value and needs this execution. For example, see opcode generated when a string literal as docstring:
2 0 LOAD_CONST 0 ('Hello world')
2 STORE_NAME 0 (__doc__)
3 4 LOAD_NAME 1 (print)
6 LOAD_CONST 1 ('Hello')
8 CALL_FUNCTION 1
10 POP_TOP
12 LOAD_CONST 2 (None)
14 RETURN_VALUE
None
Note that it stores the string inside the object __doc__
, be it the module, class or function. However, when reporting a f-string, the result is:
2 0 LOAD_CONST 0 ('Hello ')
2 LOAD_NAME 0 (name)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 POP_TOP
3 10 LOAD_NAME 1 (print)
12 LOAD_CONST 1 ('Hello')
14 CALL_FUNCTION 1
16 POP_TOP
18 LOAD_CONST 2 (None)
20 RETURN_VALUE
None
That is, the interpreter will evaluate the f-string, interpolation of values and return to string end that as it will no longer be a docstring, will be a string in the code and therefore discarded. How is not informed a string literally, the function ends up being without a docstring, so return None
.
Because Guido said so! Now that he’s not in charge, he can use it! : Q :D I believe it is a limitation because it can only take a constant value. When using interpolation that is an action to be executed at the time of execution to give a result. It is worth as a response to elaborate a little more?
– Maniero
It is, especially if you have sources. This is the way. A f-string is no longer literal because it needs to treat interpolation and, by definition, cannot be used as a docstring.
– Woss
I think so, I found justification :)
– Maniero
Until because the docstring is generated at "compile" time, not runtime, then how would you guarantee the value of the variable? In this case it has value, but imagine using a parameter inside the docstrings...
– Woss