0

I'm trying to access variables I created in one function inside another module in Python to plot a graph however, Python can't find them. Heres some example code:

class1:
    def method1
        var1 = []
        var2 = []
    #Do something with var1 and var2
        print var1
        print var2
        return var1,var2

sample = class1()
sample.method1

here is class 2

 from class1 import *

 class number2:
     sample.method1()

This does as intended and prints var1 and var2 but I can't call var1 or var2 inside class number 2

FIXED EDIT: Incase anyone else has this issue, I fixed it by importing this above class two

  from Module1 import Class1,sample

And then inside class2

      var1,var2 = smaple.method1()
cryptiblinux
  • 29
  • 3
  • 12

1 Answers1

1

The code you posted is full of syntax errors as Francesco sayed in his comment. Perhaps you could paste the correct one.

You don't import from a class but from a package or a module. Plus you don't "call" a variable unless it's a callable.

In your case you could just have :

file1.py :

class class1:
    def __init__(self): # In your class's code, self is the current instance (= this for othe languages, it's always the first parameter.)
        self.var = 0

    def method1(self):
        print(self.var)

sample = class1()

file2.py :

from file1 import class1, sample

class class2(class1):
    def method2(self):
        self.var += 1
        print(self.var)

v = class2()          # create an instance of class2 that inherits from class1
v.method1()           # calls method inherited from class1 that prints the var instance variable
sample.method1()      # same
print(v.var)          # You can also access it from outside the class definition.
v.var += 2            # You also can modify it.
print(v.var)
v.method2()           # Increment the variable, then print it.
v.method2()           # same.
sample.method1()      # Print var from sample.
#sample.method2()  <--- not possible because sample is an instance of class1 and not of class2

Note that to have method1() in class2, class2 must inherit from class1. But you can still import variables from other packages/modules.

Note also that var is unique for each instance of the class.

Community
  • 1
  • 1
vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • Thanks for the comment, but how do I access var1 and var2 from this – cryptiblinux Apr 28 '16 at 21:33
  • @javasaucebiner like a normal function unles you want this variable be related to the instance or the class. Perhaps you could be more precise about what you want. What did you really tried and how didn't it work? – vmonteco Apr 28 '16 at 21:34
  • I would like to plot a matplotlib graph, using the var1 and var2 lists created in class1 – cryptiblinux Apr 28 '16 at 21:38
  • @javasaucebiner Actually, var1 and var2 only exist when you call the method they're in. Do you want these variables to be related to the instance, or to the class perhaps? That's where you should start in your post. **And correct the code in your post please.** – vmonteco Apr 28 '16 at 21:39
  • I don't really follow "related to instance or the class". I think class. What I want to do is use lists var1 and var2, which are filled inside class1(method1) to plot a graph in matplotlib inside class2 – cryptiblinux Apr 28 '16 at 21:50
  • @javasaucebiner I edited my answer, is that what you were searching for? – vmonteco Apr 28 '16 at 22:06
  • @javasaucebiner So you meant "class1" as a file name? – vmonteco Apr 28 '16 at 22:20