I am writing a program that is utilizing multiple classes. I have one class that is dedicated to determining values for a set of variables. I would then like to be able to access the values of those variables with other classes. My code looks as follows: Show
I use classA to initialize 2 variables (var1 and var2). I then use methodA to add them, saving the result as var1 (I think this will make var1 = 3 and var2 = 2). What I want to know is how would I have ClassB then be able to get the values for var1 and var2 from ClassA?
That1Guy 6,8494 gold badges48 silver badges57 bronze badges asked Nov 15, 2013 at 4:54
1
On the other hand - if you were to use class variables, you could access var1 and var2 without sending object1 as a parameter to ClassB.
Note, however, that class variables are shared among all instances of its class.
answered Nov 15, 2013 at 4:59
Steinar LimaSteinar Lima 7,4612 gold badges37 silver badges40 bronze badges 1
Can you explain why you want to do this? You're playing around with instance variables/attributes which won't migrate from one class to another (they're bound not even to
In this scenario, I'd have to know a little bit more about your particular use case before I know if this is a course of action that I would actually recommend though... answered Nov 15, 2013 at 5:00
mgilsonmgilson 289k60 gold badges603 silver badges675 bronze badges 1 var1 and var2 is an Instance variables of ClassA. Create an Instance of ClassB and when calling the methodA it will check the methodA in Child class (ClassB) first, If methodA is not present in ClassB you need to invoke the ClassA by using the super() method which will get you all the methods implemented in ClassA. Now, you can access all the methods and attributes of ClassB.
answered Aug 29, 2020 at 14:44
1
answered Nov 15, 2013 at 5:57
0 we can access/pass arguments/variables from one class to another class using object reference.
answered Jan 19, 2020 at 5:07 Just create the variables in a class. And then inherit from that class to access its variables. But before accessing them, the parent class has to be called to initiate the variables.
answered Nov 30, 2020 at 8:40
How do you access a variable from another class in Python?Variable defined inside the class:
var_name. If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.
Can we access instance variable in another class?Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference.
How do you access an instance variable in Python?We can access the instance variable using the object and dot ( . ) operator. In Python, to work with an instance variable and method, we use the self keyword. We use the self keyword as the first parameter to a method.
How do you access instance variables?Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name.
|