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.