I'm trying to change the order of the matrix X through class P, which is already passed to a function in the base class B. The change in order does not reflect in the base class B. Is there a way to achieve this? Please see the following MWE:
#p.py
import numpy as np
import b
class P(b.B):
def __init__(self, opts):
self.opts = opts
def func1(self, X):
print('func1:',X)
def func2(self):
randomize = np.arange(len(self.myX))
np.random.shuffle(randomize)
self.myX = self.myX[randomize]
print('func2',self.myX)
def func3(self, X):
"""
X is a 2d matrix
"""
self.myX = X
#call the function from the base class
b.B.func3(self, self.myX)
#b.py
class B:
"""
base class.
"""
def __init__(self, opts):
self.opts = opts
def func3(self, X):
for n in range(2):
for i in range(X.shape[0]):
self.func1(X[i])
self.func2()
from the console:
p1 = p.P({})
X=np.array([[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8]])
p1.func3(X)
Current Output:
func1: [1 2 3]
func1: [2 3 4]
func1: [3 4 5]
func1: [4 5 6]
func1: [5 6 7]
func1: [6 7 8]
func2 [[6 7 8]
[3 4 5]
[2 3 4]
[5 6 7]
[4 5 6]
[1 2 3]]
func1: [1 2 3]
func1: [2 3 4]
func1: [3 4 5]
func1: [4 5 6]
func1: [5 6 7]
func1: [6 7 8]
Expected Output:
func1: [1 2 3]
func1: [2 3 4]
func1: [3 4 5]
func1: [4 5 6]
func1: [5 6 7]
func1: [6 7 8]
func2 [[6 7 8]
[3 4 5]
[2 3 4]
[5 6 7]
[4 5 6]
[1 2 3]]
func1: [6 7 8]
func1: [3 4 5]
func1: [2 3 4]
func1: [5 6 7]
func1: [4 5 6]
func1: [1 2 3]
So basically, when the control returns from p.func2 to func3 in B, X should be the same as self.myX. I believe this should happen because self.myX is by default passed by reference to b.func3.
Pfor me.B.func3()to call uponB.func1()andB.func2()instead ofP.func1()andP.func2()? It's not clear what you're asking.Bso I'm not sure the point of you usingb.Bas a base class. You don't even need that base class since you are directly referencingb.B.func3in yourp.P.func3anyways. If you mean to keep the methods ofb.Byou would need to name the functions differently. "Changes not reflecting in base classB" is also confusing because you really only created aPinstance with the baseB. There's no instance ofBfor you to observe any reflection - everything happens underP().