Strange behaviour class variables [SOLVED]
Posted: Thu Jul 16, 2020 12:22 pm
The problem described below was found while using the currently latest build of MicroPython on an ESP8266. A (test) class with 3 class variables is created, as well as two instances of the test class. Two class variables are modified, but only one of the two modifications is visible to both instances.
The test class, saved in file showProblem.py, is:
while the main program is:
When this script is run, the following output is generated:
Class variable cvsp_wait_time is modified using object obj0 and the modifications are visible in object obj1. However, the modification of class variable cvsp_lst_task is only visible in object obj0, not in object obj1.
My expectation is that variable cvsp_lst_task is shared between both objects, and thus will show the same value if accessed via both objects. Clearly, it does not. What is wrong in the code fragments above?
Explanatory note: This test class is an excerpt of a class in which list cvsp_wait_time will be modified quite often. Therefore, I decided not to use a list with a dynamic length in the expectation that a static list will minimise heap fragmentation. Thus methods like list.insert() are not used. Consequently, the current length of the list is maintained in cvsp_lst_task. As this variable seems to have a per-instance value rather than a per-class value, it is not possible to maintain list cvsp_wait_time properly.
The test class, saved in file showProblem.py, is:
Code: Select all
class showProblem():
cvsp_lst_task = 0
cvsp_max_task = 8
cvsp_wait_time= [None] * cvsp_max_task
def __init__( self ):
self.TimePeriod= 0
def showcv( self ):
print( self.cvsp_lst_task, self.cvsp_max_task, self.cvsp_wait_time )
def setcv( self, value ):
self.cvsp_wait_time[self.cvsp_lst_task]= value
self.cvsp_lst_task+= 1
Code: Select all
import showProblem
obj0= showProblem.showProblem()
obj0.showcv()
obj1= showProblem.showProblem()
obj1.showcv()
for j in range(3):
obj0.setcv( j )
obj0.showcv()
obj1.showcv()
Code: Select all
>>> %Run -c $EDITOR_CONTENT
0 8 [None, None, None, None, None, None, None, None]
0 8 [None, None, None, None, None, None, None, None]
1 8 [0, None, None, None, None, None, None, None]
0 8 [0, None, None, None, None, None, None, None]
2 8 [0, 1, None, None, None, None, None, None]
0 8 [0, 1, None, None, None, None, None, None]
3 8 [0, 1, 2, None, None, None, None, None]
0 8 [0, 1, 2, None, None, None, None, None]
>>>
My expectation is that variable cvsp_lst_task is shared between both objects, and thus will show the same value if accessed via both objects. Clearly, it does not. What is wrong in the code fragments above?
Explanatory note: This test class is an excerpt of a class in which list cvsp_wait_time will be modified quite often. Therefore, I decided not to use a list with a dynamic length in the expectation that a static list will minimise heap fragmentation. Thus methods like list.insert() are not used. Consequently, the current length of the list is maintained in cvsp_lst_task. As this variable seems to have a per-instance value rather than a per-class value, it is not possible to maintain list cvsp_wait_time properly.