How Would I Access Variables from One Class to Another

How would I access variables from one class to another?

var1 and var2 are instance variables. That means that you have to send the instance of ClassA to ClassB in order for ClassB to access it, i.e:

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

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

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

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB(object1)
print sum

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.

class ClassA(object):
var1 = 0
var2 = 0
def __init__(self):
ClassA.var1 = 1
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

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB()
print sum

Note, however, that class variables are shared among all instances of its class.

How can I pass a variable from one class to another in Python?

self.x means a variable x just works inside the class definition.
Try this: Define a class A(object), define x=1 before init()

class A(object):
x = 1
__init__(self)
.....

You can address x like A.x.

Best way to access variables from another class?

In general there are 2 ways to access a variable from another class:

  1. You create an object of that class. Then this object has all the variables from the scope of that class assigned to it. For example:
Test t = new Test();
t.name = "test";

  1. You can also create a static variable. Then the variable is assigned to the class not the object of that class. This way you will not need to create an object, but all instances of the class will share the same variable.
//In the scope of the class
static String name;

-------------------------
//when classing the class
Test.name = "The Name of the Test";

If you do not want to create a new instance of a class every time, and always use the same instance, you can create a singleton object. You write a getter method that gets you the object. It looks like this:

public class Test {
Test t;

public static void main(String[] args) {
t = new Test();
}

public Test getTest() {
if (t != null) {
return t;
} else {
t = new Test();
return t;
}
}
}

I see you work with a JFrame. Then you will probably want to make it a singleton. Else you will open a new instance of the JFrame every time you call upon it, which is not recommended.
Does this answer your question?

Calling variable in one class file to another file in python3

Here is an alternative
pack.py

class One:
def __init__(self):
self.number = 100

def test(self):
print('test')

class Two:
def sample(self):
print('Sample')

another.py

from pack import *

class Three:
def four(self):
self.obj = One().number
return self.obj

three = Three().four()
print(three)

By what seems to be your approach, you were using classes to access variables. It is better to instantiate variables in a constructor ( init method in class One). Then import the class and access it in another class of another file.

Also, it is a good practice to name classes beginning with uppercase letters. There are more possible ways but hope it helps.

How to access one class variable from another class in python?

In your ClassB.__init__(), you're just taking a snapshot of the var1 and var2 attributes of the passed in ClassA instance. If you need to see updated values of those attributes, store the instance itself:

class ClassB(ClassA):  
def __init__(self, class_a):
self.class_a = class_a
def methodB(self):
print "Inside ClassB:",self.class_a.var1,self.class_a.var2


Related Topics



Leave a reply



Submit