I was just writing something a small code, something weird has been just happening.
class ndarray:
counter = 1
def __init__(self, array = list()):
print(array)
ndarray.counter += 1
self.array = array
print(ndarray.counter, 'ndarray.__init__', array, id(self))
def __add__(self, nd):
if len(self.array) != len(nd.array):
raise ValueError('wrong lengths')
new_nd = ndarray() # line P1
print("ndarray.__add__", new_nd, id(new_nd))
for i in range(len(self.array)):
new_nd.array.append(self.array[i] + nd.array[i])
return new_nd
def __mul__(self, nd):
if len(self.array) != len(nd.array):
raise ValueError('wrong lengths')
new_nd = ndarray() # line P2
print("ndarray.__mul__", new_nd, id(new_nd))
for i in range(len(self.array)):
new_nd.array.append(self.array[i] * nd.array[i])
return new_nd
def __repr__(self) -> str:
return str(self.array)
I have this class definition that holds a list as instance attribute and I would like to do the following operation using operator overloads:
a = ndarray([1,2,3,4,5])
b = ndarray([5,6,7,8,9])
z1 = a * b
print("z1=", z1)
Everything works fine the code above. Please pay attention to the line P2, which creates an ndarray class object with an instance attribute self.array
that holds an empty list []
, as expected. However, when I try to run the following code, which does the second operator overloading operation after the multiplication:
a = ndarray([1,2,3,4,5])
b = ndarray([5,6,7,8,9])
c = ndarray([2,3,4,5,6])
z1 = a * b
print("z1=", z1)
z2 = z1 + c
print("z2=", z2)
I do not get what I expect. The problem is that whenever the second operator overloading is called, the new_nd = ndarray()
call does not create an with empty list (although the default parameter is []
) in self.array
.
More specifically;
when the z1
operation is done, the new_nd = ndarray()
call creates a ndarray class object with an instance attribute self.array
that holds an empty list []
. However, in the second call of the operator overloading, in the z2
operation, the new_nd = ndarray()
call creates an instance attribute self.array
that holds list of the previous result z1
.
Interestingly, when I change the line P1 and line P2 as new_nd = ndarray([])
, everything works as expected.
What is going on here?