2021-12-10

Tech Programing

程式人小天地

I hace issue with __radd__ with format < ndarray> +

2 min read


I’m a beginner in Python.

I try to understand the behavior of add and radd by a short test code with my class Point.

I want use the addition < + > in différent formes like :

  1. Point + int
  2. Point + float
  3. Point + str if it’s possible to translate into int or float
  4. Point + ndarray

With def_add_( ) methode in my class’code, it’s good, I don’t have issue for each cases

BUT !!! With def_radd_( ) method in code , I obtain a strange issue only for
ndarray + Point

my code

import numpy as np
class Point():
    def __init__(self, x = 0.0, y = 0.0, z = 0.0):
        self.point = np.array([x*1.0, y*1.0, z*1.0])

    def valeur(self):
        return self.point

    def __str__(self):
        return f"point : {self.point}"

    def __add__(self, other):
        #it's OK
        pass

    def __radd__(self, other):
        print( "__radd__  >>> self = ", self, "  other = ", other,end = '')
        if isinstance(other, np.ndarray ):
            print("<array>   + <Point> ", end = '' )
            a = self.point + other*1.0
            return Point(a[0], a[1], a[2]) 

        elif isinstance(other, int):
            print("   therefore we are in case <int> + <Point> we use __radd__  ", end = '' )
            a = self.point + other*1.0
            print(a)
            return Point(a[0], a[1], a[2])

        # elif other cases for str and float . The booth run whele

        else:
            return self.point

if __name__ == "__main__":
    point1 = Point( -1, 2, 1)
    print(point1)
    print("nwhith the format np.array( [10, 20, 30 ] ) + point1.valeur() it's OK but it's not my target")
    print(np.array( [10, 20, 30 ] ) + point1.valeur())
    print("nwhith the format ndarray + Point it's NOK. I obtain 3 calls of __radd__ for each elements of ndarray")
    print( "n result IT'S NOT OK  ==> n", np.array( [10, 20, 30 ] ) + point1 )

The output is :

 point : [-1.  2.  1.]

 whith the format np.array( [10, 20, 30 ] ) + point1.valeur() it's OK but it's not my target
 [ 9. 22. 31.]

 whith the format ndarray + Point it's NOK. I obtain 3 calls of __radd__ for each elements of ndarray

 __radd__  >>> self =  point : [-1.  2.  1.]   other =  10   therefore we are in case <int> + <Point> we use __radd__  [ 9. 12. 11.]

 __radd__  >>> self =  point : [-1.  2.  1.]   other =  20   therefore we are in case <int> + <Point> we use __radd__  [19. 22. 21.]

 __radd__  >>> self =  point : [-1.  2.  1.]   other =  30   therefore we are in case <int> + <Point> we use __radd__  [29. 32. 31.]

 result IT'S NOT OK  ==>  
 [<__main__.Point object at 0x00000214E00BBA30>
  <__main__.Point object at 0x00000214E00E5EB0>
  <__main__.Point object at 0x00000214EEDC3C40>]

The list of 3 address are in relation respectively with [ 9. 12. 11.], [19. 22. 21.] and [29. 32. 31.]

I was surprised: I was thinked that the used test will be true : if isinstance(other, np.ndarray ):

My question is : Why the left ndarray give each element instead an full ndarray



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *