0

How to access variable from one class to another class. Lets say class A has argument given from command line. I want to access the variable of class A give through command line to another class say class B.How to access?

class A():
    def __init__(self,var1,var2):
        pass
class B():
    def __call__(self):
        #(here I want to use var1 and var2 from class A)

A(sys.argv[1],sys.argv[2])
6
  • You could inherit B from A, or pass an instance of A to B when constructing it. Whether or not to inherit B from A may depend on how functionally similar B is to A. It's probably easier (and, without context, more logical), to pass an instance of A to the constructor of B. Commented Nov 27, 2017 at 6:34
  • def __call__(self): or def __init__(self):? That's quite a difference. Commented Nov 27, 2017 at 6:35
  • What exactly do you mean by "use"? Do something with the same arguments a particular instance of A was constructed with? Commented Nov 27, 2017 at 6:35
  • Also what do you think the line A(sys.argv[1],sys.argv[2]) is doing? Commented Nov 27, 2017 at 6:36
  • 1
    "I want to access the variable of class A give through command line to another class" These arguments are still stored in sys.argv[1] and sys.argv[2], so why not just use them again? Commented Nov 27, 2017 at 6:39

3 Answers 3

3

Either

import sys
class A():
    def __init__(self,var1,var2):
        self.var1 = var1
        self.var2 = var2
        pass
class B():
    def __call__(self, a):
        print(a.var1, a.var2)
        #(here I want to use var1 and var2 from class A)

a = A(sys.argv[1],sys.argv[2])
b = B()
b(a)

Or

import sys
class A():
    def __init__(self,var1,var2):
        self.var1 = var1
        self.var2 = var2
        pass
class B():
    def __init__(self, a):
        self.a = a
    def __call__(self):
        print(self.a.var1, self.a.var2)
        #(here I want to use var1 and var2 from class A)

a = A(sys.argv[1],sys.argv[2])
b = B(a)
b()

use One among these two based on use case

Sign up to request clarification or add additional context in comments.

Comments

0

First create instance variables for class A by changing its definition as follows

class A():
    def __init__(self,var1,var2):
        self.var1 = var1
        self.var2 = var2

and then pass the instance of class A to __call__ method of class B as follows

class B():
    def __init__(self, class_A_object):
        self.var1 = class_A_object.var1
        self.var2 = class_A_object.var2

Now you can use the var1 and var2 of class B anywhere inside the instance you created.

object_a = A(var1, var2)
object_b = B(object_a)
print object_b.var1, object_b.var2

Comments

0

Since var1 and var2 are instance variables, you could send the instance of class A to class B:

import sys

class A():
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2

class B():        
    def __call__(self, A):
        self.var1 = A.var1
        self.var2 = A.var2

obj_one = A(sys.argv[1], sys.argv[2])
obj_two = B()

obj_two(obj_one)

print(obj_two.var1)
print(obj_two.var2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.