dtr43
dtr43

Reputation: 155

Using a variable from one class to another one in python

I want to use a variable from class A for some computation in class B. I,m not sure that I use the self.out from the class A in class B appropriately?

Class A:

class A(nn.Module):
    def __init__(self):
        super(A, self).__init__()
        
        self.out = func()

Class B:

class B(nn.Module):
    def __init__(self):
        super(A, self).__init__()
        
        self.result = function_1() + A.self.out

Upvotes: -1

Views: 75

Answers (1)

Rafael MR
Rafael MR

Reputation: 303

Maybe this is what you need. I made a small example of what I understood. These "prints" were placed to improve the understanding that Class "C" can fetch any function or variable from the other parent classes.

class A():
    def __init__(self):
        variable = None
    def test(self, number):
        return f'another class {number}'

class B():
    def __init__(self):
        self.data = None
        self.out = self.print_data(5)
    def print_data(self, number):
        return number
    def print_elem(self):
        return self.data
class C(A, B):
    def __init__(self):
          super().__init__()
        
c = C()

print(c.print_data(8))
print(c.out)
c.data = 100
print(c.print_elem())

print(c.test(3))

Upvotes: 2

Related Questions