Radek D
Radek D

Reputation: 103

How to pass class instance and all it does to another class in Python

I am writing a script with file structure as below:

The field_integration.py:

import numpy as np
class Integration():
    def __init__(self, param1, param2):

        self.p1 = param1
        self.p2 = param2

        self.derive(self.p2)

    def derive(self, param2):

        self.pp1 = 2/self.p1
        self.pp2 = param2/3

        self.another_function_inside_the_class()
    
    def another_function_inside_the_class(self):
        print('OK')

The main.py:

from field_int import Integration
from fitting import Autofit

ppp1 = 1
ppp2 = 2

profile = Integration(ppp1, ppp2)

ppp5 = 3
fitt = Autofit(ppp5)

y_pre_fitted = fitt.pre_fitting(profile.p2, 0)

The fitting.py:

from field_int import Integration

class Autofit(Integration):
    def __init__(self, param5):
        self.p5 = param5
        
    def pre_fitting(self, param2, param3):
        Integration.derive(param2)
        print(param3)

While fitting.py pre_fitting method works well when directly in main.py, here it throws the TypeError: line 8, in pre_fitting Integration.derive(param2) TypeError: derive() missing 1 required positional argument: 'param2'.

If I change the line to have Integration.derive(self, param2), it throws the AttributeError: 'Autofit' object has no attribute 'p1'

How then can I recreate the functionality of the code from the main.py inside another file? Should I move it to the same file as the class definition I instantiate? I am quite a noob concerning classes, I heard something about inheritance but I was only beating my head against the wall, because I don't understand how that would help me.

Upvotes: 0

Views: 1991

Answers (1)

Yatharth Ranjan
Yatharth Ranjan

Reputation: 471

Since you are subclassing Integration, you can just call the function with self or super(). super() can be used when the subclass also overrides the superclass' method and we need to call the superclass method instead of the subclass. But in your case, you are not overriding so a simple self call should suffice, so -

from field_int import Integration

class Autofit(Integration):
    def __init__(self, param5):
        self.p5 = param5
        
    def pre_fitting(self, param2, param3):
        self.derive(param2)
        print(param3)

On another note, it is usually a good idea to also Initialise the superclass using super() such as -

from field_int import Integration

class Autofit(Integration):
    def __init__(self, param1, param2, param5):
        super().__init__(param1, param2)
        self.p5 = param5
        
    def pre_fitting(self, param2, param3):
        self.derive(param2)
        print(param3)

Because now if you call the derive method without the super().__init__ the values for p1 and p2 required in the derive method will not be defined. So better to call __init__ on superclass before the method call.

Update

An alternate approach to what you want to achieve without using inheritance (if you are not extending the functionality of Integration class) -

fitting.py

from field_int import Integration

class Autofit():
    def __init__(self, integration: Integration, param5):
        self.p5 = param5
        self.integration = integration
        
    def pre_fitting(self, param2, param3):
        self.integration.derive(param2)
        print(param3)

main.py

from field_int import Integration
from fitting import Autofit

ppp1 = 1
ppp2 = 2

profile = Integration(ppp1, ppp2)

ppp5 = 3
fitt = Autofit(profile, ppp5)

y_pre_fitted = fitt.pre_fitting(profile.p2, 0)

Upvotes: 1

Related Questions