I am trying import global variables defined in one script to other script.
main.py
# import ipdb;ipdb.set_trace()
import sample
expr = ['age < 10', "country == 'China'"]
values = {}
class Test:
def __init__(self, name, age, country) :
self.name = name
self.age = age
self.country = country
global values
values['name'] = name
values['age'] = age
values['country'] = country
if __name__ == "__main__" :
name = raw_input("Enter your name:")
age = int(input("Enter your age:"))
country = raw_input("Enter your country:")
user = Test(name, age, country)
sample.printValExp()
sample.py
import main
def printValExp():
print(main.expr)
print(main.values)
# print(main.Test.name)
O/P:
[root@dev01 dynamic]# python main.py
Enter your name:sunny
Enter your age:14
Enter your country:japan
['age < 10', "country == 'China'"]
{}
[root@dev01 dynamic]#
In main.py, I defined 2 global variables
--> expr, initialized with list
--> values, as empty dict
uninitialized values dictionary, I am populating with user entered details in constructor(__ init __)
Though this values dict is populated in init method of main.py, when i imported this global variable in other script sample.py, it is showing empty there
But when tried to print expr, which is also global variable intialized while declaring itself in main.py, this is printing expr value(list of strings)
What should I do to get global variable values in other script, whose values are populated in
__ init __
or inside
if __ name __ == '__ main __' :