I have a module and two classes which are called A and B. I would like to record some functions from duvisiopy.model_functions module into class B by using class A then return B. As you can see the output1, they are recorded and they are runnable.
#duvisiopy.model_functions
def compile(self,
loss_function,
optimizer):
self.loss_function = loss_function
self.optimizer = optimizer
import types
import duvisiopy.model_functions
class B():
def __init__(self):
self.b = "b"
class A():
def __init__(self):
self.a = "a"
def __new__(self, cls):
for attr_name in dir(duvisiopy.model_functions):
if attr_name not in dir(cls) and not attr_name.startswith("_"):
attr = getattr(duvisiopy.model_functions, attr_name)
if not isinstance(attr, types.ModuleType):
attr = attr.__get__(duvisiopy.model_functions,
duvisiopy.model_functions.__class__)
print(attr_name)
setattr(cls, attr_name, attr)
return cls
b = B()
a = A(b)
a.compile("test1","test2")
for i in dir(a):
if not a.starsith("_"):
print(i)
output1 is
compile
evaluate
fit
progressbar
However, If I try to reach a.optimizer, it throws error
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
a.optimizer
AttributeError: 'B' object has no attribute 'optimizer'
What is the problem ?
UPDATE 1
if I set compile function to return self
#duvisiopy.model_functions
def compile(self,
loss_function,
optimizer):
self.loss_function = loss_function
self.optimizer = optimizer
return self
c = a.compile("test1","test2")
it prints
'absolute_path\\duvisiopy\\model_functions.py'
and if I try to reach optimizer by c, it prints,
print(c.optimizer) #--> 'test2'