Author ncoghlan
Recipients Arfrever, belopolsky, ezio.melotti, gvanrossum, ilblackdragon, martin.panter, ncoghlan, o11c, pitrou, rbcollins, smurfix, xonatius
Date 2015-08-26.09:59:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
With __qualname__ being mutable, I agree that adding __code__.co_qualname wouldn't be a substitute for that. Instead, similar to the relationship between __name__ and __code__.co_name, they would start out the same, but the function attribute may change later (e.g. through the use of functools.wraps).

However, that also highlights why we need to use the compile time qualname in the traceback, rather than the runtime one: because we currently use the compile time name from the code object rather than the runtime name from the function.

A test case to demonstrate that:

>>> def f():
...     1/0
>>> import functools
>>> @functools.wraps(f)
... def g():
...     return f()
>>> g()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in g
  File "<stdin>", line 2, in f
ZeroDivisionError: division by zero
>>> g.__qualname__
>>> g.__name__

Note that "g" still appears in the relevant traceback line, even though both __name__ and __qualname__ have been updated to refer to "f". For a traceback we want to know where the source of the function actually lives, not where it claims to be from for human introspection purposes.

As far as the comparison to __module__ goes, I think that's a different case - we couldn't add that to the code object even if we wanted to, because the compiler doesn't know the module name, it only knows the file name. It's also possible to take an already compiled python file, move it to a different directory, and have the module name change due to the new location in the package hierarchy. __qualname__ is different as, like __name__, it's entirely local to the module and hence available to the compiler at compile time.
Date User Action Args
2015-08-26 09:59:55ncoghlansetrecipients: + ncoghlan, gvanrossum, belopolsky, pitrou, rbcollins, ezio.melotti, smurfix, Arfrever, martin.panter, o11c, ilblackdragon, xonatius
2015-08-26 09:59:55ncoghlansetmessageid: <>
2015-08-26 09:59:55ncoghlanlinkissue2786 messages
2015-08-26 09:59:54ncoghlancreate