-1
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 __' :
Waageesh
  • 17
  • 5

1 Answers1

0

I don't know why exactly above is not working,

any explanations are totally welcome

But i have found work-around solution for given problem, where unable to extend the scope of global variable, whose value is still showing NULL, though its value is populated in __ init __ ()

I create another file values.py and implement extern variables concept just like in C I followed this to design extern variables concept in python, whiose variable value will be shared across different files

values.py

userDict = {}

main.py

import sample
import values


expr = ['age < 10', "country == 'China'"]

class Test:
    def __init__(self, name, age, country) :
        self.name = name
        self.age = age
        self.country = country
        values.userDict['name'] = name
        values.userDict['age'] = age
        values.userDict['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
import values

def printValExp():
    print(main.expr)
    print(values.userDict)
    # print(main.Test.name)

O/P:

[root@dev01 dynamic]# python main.py
Enter your name:sunny
Enter your age:15
Enter your country:japan
['age < 10', "country == 'China'"]
{'country': 'japan', 'age': 15, 'name': 'sunny'}
Waageesh
  • 17
  • 5