__qualname__ in python 3.3

Antoine Pitrou antoine at python.org
Sun Sep 7 09:03:04 EDT 2014


Hi,

ISE Development <isenntp <at> gmail.com> writes:
> 'code' object     'function' object
> ----------------  ------------------------------------
> co_name: test     __qualname__: test
> co_name: T        __qualname__: T
> co_name: method   __qualname__: test.<locals>.T.method
> 
> The second call corresponds to the class definition and not the call to the 
> constructor (which is in fact a call to 'object.__init__', a C function 
> hence not traced as a 'call' event - I checked this by disassembling the 
> code object).

There's nothing wrong here. That's just the way things are implemented
internally. This may change in the future without prior notice, so
you shouldn't rely on it.

If you want to dig more, you have to look at how the hidden function ("T")
works:

>>> def f():
...   class T: pass
... 
>>> f.__code__.co_consts
(None, <code object T at 0x7f4d9d0f4a00, file "<stdin>", line 2>, 'T')
>>> dis.dis(f.__code__.co_consts[1])
  2           0 LOAD_NAME                0 (__name__)
              3 STORE_NAME               1 (__module__)
              6 LOAD_CONST               0 ('f.<locals>.T')
              9 STORE_NAME               2 (__qualname__)
             12 LOAD_CONST               1 (None)
             15 RETURN_VALUE

Regards

Antoine.





More information about the Python-list mailing list