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)]])
3 Answers 3
You need an instance of A:
def method_B(self, the_a):
return the_a.a * self.c
1 Comment
To follow on the previous answer, if you create an instance of A:
a = A(a, b)
then, in class B, you can pass in that instance as a single argument and access any method(or variable for that matter) by referring to it's instance:
class B():
def __init__(self, c, d, instance_of_a):
self.c = c
self.f = d
self.e = instance_of_a.[the method or variable you want from class A]
Hope this helps,
Hold an instance of class A in class B.
class A():
def __init__(self, a, b):
self.a = a
self.b = b
def method_A(self):
return self.a
class B():
def __init__(self, c, d, the_a):
self.c = c
self.d = d
self.the_a = the_a
def method_B(self):
return self.the_a.a * self.c
a = A(1, 2)
b = B(3, 4, a)
3 Comments
a = np.array([[A(2, 4), A(3,5)]]) and a loop in method_B but I have an error about accessing the attribute from class B init.
AinByou're going to have to pass the arguments one way or anotherAinstances, do you wantBto have multipleA? If not then just giveBa singleAinstance instead of a list of them. Otherwise yourB.__init__needs to handle multipleAinstances.self.the_aV1 = the_a.aandself.the_aV2 = the_a.bisn't going to work asthe_ais a numpy array which as the error message states doesn't have an attributea. What do you wantthe_aV1andthe_aV2to be since you want multipleA?