I have a class:
class A():
def __init__(self, a, b):
self.a = a
self.b = b
def method_A(self):
return self.a
Now, I have another class B:
class B():
def __init__(self, c,d):
self.c = c
self.d = d
def method_B(self):
# do computations with a and b from class A
# return A.a * c
One solution is to have B inherit from A but that means that it inherits all attributes of A which is something that I don't want.
For example it will be :
class B(A):
def __init__(self,a,b,c,d)
A().__init__(self,a,b)
self.c = c
self.d = d
So, I have to initialize my B class with def __init__(self,a,b,c,d)
all arguments (from A and for B) , right?
Is there a way I can avoid that ?And still have access to class A attributes and methods?
---------- UPDATE 1 --------------------------------------
Updated according to the answers.
I want to be able to use an array of class A as input to class B.
I tried:
import numpy as np
class B():
def __init__(self, c, d, the_a):
self.c = c
self.d = d
self.the_a = the_a
def method_B(self):
for i in self.the_a:
return self.the_a[0][0].a * self.c
a = np.array([[A(2, 4), A(3,5)]])
b = B(4, 4, a)
print(b.method_B())
and I am receiving : the value 8
.
But obvious I am not correctly using the loop in method_B.
I am using only the 0 index, but I can't figure!
------- UPDATE 2 ------------------------------
I am almost there..
def method_B(self):
for idx, x in np.ndenumerate(self.the_a):
self.the_a[idx].a = self.the_a[idx].a * self.c
self.the_a[idx].b = self.the_a[idx].b * self.c
return self.the_a
Now, it returns me [[<__main__.A object at 0x7f165426c9b0>
<__main__.A object at 0x7f165426c898>]]
.
Is there a way I can receive the updated array like:
np.array([[ A(8, 16), A(12,20)]])