5

I've 3 files. One is for defining the variables and other two contain the required modules.

variable.py

my_var = ""

test.py

import variable

def trial():
    variable.my_var = "Hello"

main.py (not working)

from variable import *
from test import trial

if __name__ == "__main__":
    trial()
    print my_var

And when I run main.py, it gives nothing. However, if I change main.py like this,

main.py (working)

import variable
from test import trial

if __name__ == "__main__":
    trial()
    print variable.my_var

And it gives me the expected output i.e. Hello.

That variable.py, in my case, contains more variables and I don't want to use variable.<variable name> while accessing them. While modifying them, using variable.<variable name> is not a problem as I'm gonna modify only once depending on the user's input and then access them across multiple files multiple times rest of the time.

Now my question is, is there a way to extract all the variables in main.py after they are modified by test.py and use them without the prefix variable.?

7
  • 1
    If you do from variable import * you are adding the names from the module variable to the local namespace, hence they references point to the values of the variables when the import happens. If now you change the original module, that change will not be reflected in your imported symbols. Commented Jul 28, 2016 at 9:24
  • @dhke Yeah that I understood from working of those two variants. But my question is somewhat different. Commented Jul 28, 2016 at 9:25
  • 2
    Related question. Good answer. Commented Jul 28, 2016 at 9:26
  • @RatDon I don't understand the rationale. What's so bad about variable.? Commented Jul 28, 2016 at 9:26
  • @dhke, Using variable. will be better readable, I know. But using this 100 times is kind of not necessary when one knows there won't be any local variables and all variables used are coming from one single file. Commented Jul 28, 2016 at 9:28

1 Answer 1

5

What you want to achieve is not possible. from MODULE import * imports all the variables names and the values into your namespace, but not the variables themselves. The variables are allocated in your local namespace and changing their value is therefore not reflected onto their origin. However, changes on mutable objects are reflected because the values you import are basically the references to instances.

That is one of the many reasons why unqualified imports using the above construct are not recommended. Instead, you should question your program structure and the design decisions you took. Working with a huge amount of module-level variables can become cumbersome and very inconvenient in the long term.

Think about using a more structured approach to your application, for example by using classes.

Again I would like to point out and give credits to this thread which, in essence, addresses the same problem.


EDIT For the sake of completeness. It is possible, but this approach is not recommended. You would have to re-import the module every time you apply modifications to its non-referential variables:

from variable import *  # optional
from test import trial

if __name__ == "__main__":
    trial()
    from variable import *  # simply re-import the variables...
    print my_var
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.