numpy array subclass unexpedly shares attributes across instances

后端 未结 1 1416
死守一世寂寞
死守一世寂寞 2020-12-17 20:39

I am having a weird subclass numpy.ndarray issue that feels like
Values of instance variables of superclass persist across instances of subclass
But I have not been

相关标签:
1条回答
  • 2020-12-17 21:04

    The problem is here:

    def __new__(cls, input_array, attrs={})
    

    Never do this attrs={} in a function header. The expected result is (probably) not what you think it is. This is a common Python Pitfall. See here Default Parameter Values in Python

    The right way how to do this:

    def __new__(cls, input_array, attrs=None):
        if attrs is None:
            attrs = {}
    
    0 讨论(0)
提交回复
热议问题