John Lapoya
John Lapoya

Reputation: 592

Share a variable between classes

I'm trying to use the same variable (a list) in different classes (this is a basic example of my code, but basically is the same functionality, get a list in Class1 and then use it in Class2):

class Class1:
    def __init__(self):
        self.dosomething():
    def dosomething(self):
        x = [99, 98]
        return x

class Class2:
    def __init__(self):
        y = Class1()
        print y 

But when I call Class2 instead print the list [99, 98], I get something like this:

<myfile.Class1 instance at 0xb74c59ac>

Any idea if it is possible to share how to do it? I'm doing something wrong but I can't find in the documentation.

Upvotes: 2

Views: 264

Answers (2)

deweyredman
deweyredman

Reputation: 1450

this is because you haven't called the "dosomething" method form class1.

ACTUALLY i was wrong, you have, you just needed to call it self.x instead of x

ALso, you would want to call y.x to access the array, and instead of x =...you need self.x =, and it needs to go in the init. I'll modify your code for you.

class Class1:
  def __init__(self):
    self.dosomething()
  def dosomething(self):
    self.x = [99, 98]


class Class2:
  def __init__(self):
    y = Class1()
    print y.x 

I'd actually just recommend doing this for class1:

class Class1:
  def __init__(self):
    self.x = [99, 98]

and for Class2:

class Class2:
  def __init__(self):
   self.y = Class1().x

Upvotes: 2

zmo
zmo

Reputation: 24812

But when I call Class2 instead print the list [99, 98], i get something like this:

this is actually what you shall expect. You're allocating Class1() and assigning it to y when you instanciate Class2.

now if you run the following code:

class Class1:
    def dosomething(self):
        x = [99, 98]
        return x

class Class2:
    def __init__(self):
        y = Class1()
        print y.dosomething()

you'll get:

>>> Class2()
[99, 98]

as output. Because you're instanciating Class1, and then calling the dosomething() on y and print the result.

Though, now, you have to understand that once Class2()'s constructor has finished executing, y does not exists anymore, as well as x. If you want to keep values, you need to use member of a class, which would look like:

class Class1:
    def __init__(self):
        self.x = [99, 98]

    def get_x(self):
        return self.x

class Class2:
    def __init__(self):
        self.y = Class1()

    def get_x(self):
        print self.y.get_x()

so then you can do:

>>> z = Class2()
>>> print(z.get_x())
[99, 98]
>>> print(z.get_x())
[99, 98]

where [99, 98] is the value contained in self.x of Class1.

I hope some things got a bit more clear, and it's time for you to open a book about OOP with Python, if that's what you're already doing, please read the next chapter! :-)

You have to understand that the whole point of classes in Object Oriented Programming is to actually encapsulate the data, exposing it through a behavior. So when you say you want to share a variable from one class to the other, there is something fundamentally wrong! That's why I'm showing you a way to exchange the data referenced by a variable between classes, without actually accessing a member of one class from the other.

Im doing something wrong but i cant find in the documentation.

Thing is, it all depends on what you're really trying to do. And don't tell me you're trying to share a variable etc… It's only technical details. The real thing is the problem you're trying to solve.

Upvotes: 1

Related Questions