0

I would like to import a refence to an object, with its value updated after the initial file which imported the reference has been parsed by python. For example:

runner.py

from store import modify_var
from importer import a_function

print('>>runner')
modify_var()
a_function()

store.py

my_var = None

def modify_var():
    global my_var
    my_obj = MyObj()
    my_var = my_obj
    print('>>store, my_var has been set to: {0}'.format(my_var))

class MyObj(object):
    x = 2

importer.py

from store import my_var

print('>>importer, my_var outside function (before has been set): {0}'.format(my_var))

def a_function():
   print('>>importer, my_var after is has been set: {0}'.format(my_var))

console output after running runner.py

>>importer, my_var outside function (before has been set): None
>>runner
>>store, my_var has been set to: <tests.store.MyObj object at 0x7fbfc683a1d0>
>>importer, my_var after is has been set: None

I am trying to figure out how I can import my_var such that when I call it in a function it's value is not None.

proximacentauri
  • 1,749
  • 5
  • 25
  • 53
  • 1
    Can you define `a_function` to accept an argument, then just pass the *updated* variable when it is called? None of your functions return anything maybe you should explore that a bit. – wwii Dec 04 '17 at 22:39

1 Answers1

0

I think it doesn't like the fact that you declare my_var after giving it a value in store.py. Try one of the following:

Inside store.py, make another method that will return my_var as none:

def my_var_none():
    my_var = None
    return my_var

Then, leave runner.py as it is, and inside importer.py:

from store.py import my_var_none()
Dlamini
  • 285
  • 1
  • 9