OK I've figured out how to answer the main question. Issuing
causes micropython to emit the bytecode. I did this with these two versions:
Code: Select all
class ClassA():
CONST_1 = const(1)
def __init__(self):
self.some_property_1 = self.CONST_1 # Fails here with leading _: no attribute
a = ClassA()
and
Code: Select all
class ClassA():
CONST_1 = 1 #const(1)
def __init__(self):
self.some_property_1 = self.CONST_1 # Fails here with leading _: no attribute
a = ClassA()
Both produced the same bytecode. See my comments on the relevant lines:
Code: Select all
Raw bytecode (code_info_size=5, bytecode_size=17):
10 06 01 69 60 54 32 00 10 02 34 02 16 02 11 02
34 00 16 03 51 63
arg names:
(N_STATE 3)
(N_EXC_STACK 0)
bc=0 line=1
bc=9 line=4
bc=9 line=7
00 LOAD_BUILD_CLASS
01 MAKE_FUNCTION 7f356daa5460
03 LOAD_CONST_STRING 'ClassA'
05 CALL_FUNCTION n=2 nkw=0
07 STORE_NAME ClassA
09 LOAD_NAME ClassA
11 CALL_FUNCTION n=0 nkw=0
13 STORE_NAME a
15 LOAD_CONST_NONE
16 RETURN_VALUE
File rats23.py, code block 'ClassA' (descriptor: 7f356daa5460, bytecode @7f356daa5680 22 bytes)
Raw bytecode (code_info_size=5, bytecode_size=17):
00 06 02 28 43 11 04 16 05 10 02 16 06 81 16 07
32 00 16 08 51 63
arg names:
(N_STATE 1)
(N_EXC_STACK 0)
bc=0 line=1
bc=8 line=2
bc=11 line=4
00 LOAD_NAME __name__
02 STORE_NAME __module__
04 LOAD_CONST_STRING 'ClassA'
06 STORE_NAME __qualname__
08 LOAD_CONST_SMALL_INT 1 # Storing 1 to class variable
09 STORE_NAME CONST_1
11 MAKE_FUNCTION 7f356daa5220
13 STORE_NAME __init__
15 LOAD_CONST_NONE
16 RETURN_VALUE
File rats23.py, code block '__init__' (descriptor: 7f356daa5220, bytecode @7f356daa56a0 14 bytes)
Raw bytecode (code_info_size=6, bytecode_size=8):
11 08 08 09 60 20 b0 13 07 b0 18 0a 51 63
arg names: self
(N_STATE 3)
(N_EXC_STACK 0)
bc=0 line=1
bc=0 line=4
bc=0 line=5
00 LOAD_FAST 0
01 LOAD_ATTR CONST_1 # Explicit load of class variable
03 LOAD_FAST 0
04 STORE_ATTR some_property_1
06 LOAD_CONST_NONE
07 RETURN_VALUE
Note that a version with a leading underscore fails to compile: the class variable is not visible to __init__().