I would like to know why does python makes a LOAD_CONST with the same value (the class name) twice when a class is defined. When I run this code:
from dis import dis
dis("class A(): pass")
This is the output I get:
1 0 LOAD_BUILD_CLASS
2 LOAD_CONST 0 (<code object A at 0x0000021DCE681B70, file "<dis>", line 1>)
4 LOAD_CONST 1 ('A')
6 MAKE_FUNCTION 0
8 LOAD_CONST 1 ('A')
10 CALL_FUNCTION 2
12 STORE_NAME 0 (A)
14 LOAD_CONST 2 (None)
16 RETURN_VALUE
Disassembly of <code object A at 0x0000021DCE681B70, file "<dis>", line 1>:
1 0 LOAD_NAME 0 (__name__)
2 STORE_NAME 1 (__module__)
4 LOAD_CONST 0 ('A')
6 STORE_NAME 2 (__qualname__)
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
As you can see in the line 3 and 5 there are two LOAD_CONST with the class name.
Why make a second LOAD_CONST with the same data if the class name was already loaded? Does this has something to do with the MAKE_FUNCTION between those LOAD_CONST?
I'm running this on python 3.7.4 64-bit