I want to access the variables from one class to another class?

Submitted 3 years, 7 months ago
Ticket #180
Views 230
Language/Framework Python
Priority High
Status Closed

I have the following code:

class ClassA(object):
    def __init__(self):
        self.var1 = 5
        self.var2 = 2

    def methodA(self):
        self.var1 = self.var1 * self.var2
        return self.var1


class ClassB(ClassA):
    def __init__(self):
        self.var1 = ?
        self.var2 = ?

object1 = ClassA()
product = object1.methodA()
print sum

I want to access the variables of classA to classB also.

Submitted on Sep 28, 20
add a comment

1 Answer
1

1

Verified

class ClassA(object):
    var1 = 0
    var2 = 0
    def __init__(self):
        ClassA.var1 = 5
        ClassA.var2 = 2

    def methodA(self):
        ClassA.var1 = ClassA.var1 * ClassA.var2
        return ClassA.var1



class ClassB(ClassA):
    def __init__(self):
        print (ClassA.var1)
        print( ClassA.var2)
print("values of class B  variables before methodA")        
object2 = ClassB()

object1 = ClassA()
print("values of class B  variables before methodA and entering into funcction")
object2 = ClassB()
pro = object1.methodA()

print( "product is:",pro)
print("values of class B  variables after entering into methodA ")
object2 = ClassB()

This is the required code.

Submitted 3 years, 7 months ago

Thank you so much for your help...

- Soudhamini 3 years, 7 months ago


Latest Blogs