Oblivion_21
Oblivion_21

Reputation: 13

How to call variables from other files python with classes?

Hi all I would like to know how to call variables (which are inside classes) from other python files. I am aware of the bellow method of doing this however it will not work if the class is called from the original file.

 from (insert_file_name_hear) import *

This a similar sample to what I'm working with:

functions.py

 num = 0
 num2 = 0

 class Test():

       def alt_num(self):
           global alt_num
           alt = 55

       def change(self):
           global num, num2
           num += alt_num
           num2 = num

def print_num():
     global num2
     print(num2)

def work():
     Test.alt_num(Test)
     Test.change(Test)
     print_num()

print.py

from functions import *

work()

def printing():
     print(num2)

printing()

When I run print.py it will accuratly print in the functions.py file however it will print 0 in the print.py file. Note: Both files are in the same folder and I am running this in Python 3.

Thanks

Upvotes: 1

Views: 917

Answers (3)

Simon Black
Simon Black

Reputation: 923

Instead of using Globals you can use class variables with a little bit of effort

functions.py

class Test():
    num = 0
    num2 = 0
    alt = 0

    @classmethod
    def alt_num(cls):
        cls.alt = 55

    @classmethod
    def change(cls):
        cls.num += cls.alt
        cls.num2 = cls.num


def print_num():
     print(Test.num2)

def work():
     Test.alt_num()
     Test.change()
     print_num()
     Test.change()
     print_num()
     Test.change()
     print_num()
     return Test.num2

print.py

from functions import *

work()

def printing():
    print(Test.num2)

printing()

Note: the output of the above is

55

110

165

165

The first three come from work(), the last from printing()

Upvotes: 2

jartecken
jartecken

Reputation: 61

In functions.py you declare num2 = 0, the rest of the manipulation of num2 is made inside functions that do not return the new value.

The function print_num() actually prints the new value of num2, this is where 55 comes from in the output.

In print.py you are printing the num2 that is declared on line 2 of functions.py

If you are only interested in num2 after the value has been updated you could skip the printing in functions.py, instead return the value and print it from print.py.

It could look something like this.

functions.py

num = 0

class Test():
    def alt_num():
        global alt
        alt = 55

    def change():
        global num
        global num2
        num += alt
        num2 = num


def work():
    Test.alt_num()
    Test.change()
    return(num2)

print.py

from functions import *

def printing():
    print(work())

printing()

Upvotes: 0

Abhijeetk431
Abhijeetk431

Reputation: 846

First - alt and alt_num are not defined Second - You need to return the variable through a function to use it elsewhere

Below is how you can do this

functions.py

alt = 0
num = 0
num2 = 0

class Test():

      def alt_num(self):
          global alt
          alt = 55

      def change(self):
          global num, num2
          num += alt
          num2 = num

def print_num():
     global num2

def work():
     Test.alt_num(Test)
     Test.change(Test)
     print_num()
     return num2

print.py

from functions import *

def printing():
    print(work())

printing()

Upvotes: 0

Related Questions