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?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.
How do I grab variables from another class?There are two ways to get variables from another class.. Create an object of another class in the main class.. Extend another class in the main class.. How do you access a variable outside a function in Python?Use the object attribute syntax to access a variable outside of a function. In a function named func , use the syntax func. variable = value to store value in variable as an attribute of func . To access value outside of func , use func() to run func , then use the syntax function_name.
How do I access a variable of one method inside another method in the same class in Python?Basically, you want to store the variables you want to pass between methods as members of the class. You do this by assigning an attribute of the self parameter to the functions.
|