A private list variable is inadvertently shared between instance objects

前端 未结 2 469
执笔经年
执笔经年 2021-01-26 02:11

I created many instances of a PlotHandler class. An instance must keep it\'s variables private. But the way I managed them led to a hard to detect problem,

2条回答
  •  迷失自我
    2021-01-26 02:26

    Class attributes are shared between instances. If you want to define an instance attribute (so each object have its own reference to the variable) you have to define it in __init__

    class PlotHandler(wx.Frame):
        __crop_section = None
        __projection   = None
        __crop_xcord   = None
    
        def __init__(self, **kwargs):
            self._band_data = [] #THIS IS NOT SHARED
    

提交回复
热议问题