Skip to content Skip to sidebar Skip to footer

Supply An Array Full With Objects As Argument To Another Class And Receive Back The Corrected

I want to do this. I have a class (MyInputs) where I will initialize the inputs. I will have a numpy array which will hold many instances of the class. For example, np.array([[MyI

Solution 1:

This is a simple class that tests for == (and displays self):

classFoo():
    def__init__(self,a,b):
        self.a=a
        self.b=b
    def__repr__(self):
        returnstr((self.a,self.b))
    def__eq__(self, other):
        ifisinstance(other, Foo):
            return (self.a==other.a) and (self.b==other.b)
        else:
            False

In [43]: A=Foo(1,'a')
In [44]: B=Foo(2,'a')
In [45]: A==B
Out[45]: False
In [46]: B.a=1# change values to match; diff ids
In [47]: A==B
Out[47]: True
In [48]: A
Out[48]: (1, 'a')
In [49]: B
Out[49]: (1, 'a')

an array of these objects:

In [51]: arr = np.array([Foo(1,'a'),Foo(2,'b'),Foo(1,'a')])
In [52]: arr==arr
Out[52]: array([ True,  True,  True], dtype=bool)
In [54]: arr==A
Out[54]: array([ True, False,  True], dtype=bool)
In [58]: arr
Out[58]: array([(1, 'a'), (2, 'b'), (1, 'a')], dtype=object)

Post a Comment for "Supply An Array Full With Objects As Argument To Another Class And Receive Back The Corrected"