1

I have 2 files:

Main.py

from test import test
def main():
    sol = 'hello'
    if test() == sol:
        print('Yes')
    else:
        print('No')

test.py

def test():
    return 'hello'

Is there a way to access the sol variable in my test function which is in another file? I'd like to do something like this:

def test():
    return sol

This way it's always the same as the variable in Main.py. I tried a solution which is mentioned on Python extract variables from an imported file but it didn't work for me. Thank you for any help

Edit: I'd like to do it without changing the Main.py file

6
  • Then, your output will always be Yes, what's the meaning of your code? Commented Sep 29, 2018 at 3:15
  • @lagom It's just a simple code to show my issue and what i'd like to do. I am curious if something like this is possible Commented Sep 29, 2018 at 3:17
  • Pass the variable to the function as a parameter? def test(sol): return sol Commented Sep 29, 2018 at 3:20
  • @TyloBedo would it be possible without changing the Main.py ? Commented Sep 29, 2018 at 3:21
  • You would need to pass the variable to test in Main.py if test(sol) == sol:. What are you actually trying to accomplish? I think you’re probably going about it in the wrong way. Commented Sep 29, 2018 at 3:25

3 Answers 3

2

Since sol isn't defined in the function you will need to declare it as a global function so it can be used in the function. Change test.pyto the following...

test():
    global sol
    return sol

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I am very sorry, i forgot to mention the sol variable is in a function too. Would it be possible to access it like this?
2

If sol is a constant (i.e. a variable which will not change during the course of the program), then you can put it into a separate file, say var.py, and import it into both main.py and test.py:

from var import sol

BUT what you will be importing is a copy of the variable with the value it had at the time it was imported - any subsequent reassignments will not update the value of sol in test.py and main.py. Because a string is immutable, when you reassign a value to it what you are actually doing is you are reusing the variable name for a new entity.

What you need to do is have your variable in a mutable structure, such as a list or a class, so your var.py will look like this:

class v(object):
    sol = 'Hello'

and then in main.py and test.py you can refer to sol using:

from var import v
print(v.sol)

This way, any changes to v.sol will be correctly reflected anywhere class v is imported. A bit cumbersome, but that's how it is in Python.

2 Comments

So there isn't really a way to do it without touching the main.py and creating a new file?
The cleanest way would be to put the shared variable in a separate file, which can be imported by all files which need access to the variable (via a mutable structure such as a class, list etc.) If you do not want to do that, you can put the class v in test.py, and then import it in main.py together with your test function.
1

You can use inspect module to get the sol variable from main function without change anything of main.py. Of course, you need to call main function.

Main.py:

from test import test
def main():
    sol = 'hello'
    if test() == sol:
        print('Yes')
    else:
        print('No')

main()

test.py:

import inspect

def test():
    frame = inspect.currentframe()
    sol_from_main = 'default'
    try:
        sol_from_main = frame.f_back.f_locals['sol'] # line A
    except:
        print('No sol found')
    print(sol_from_main)  # this print 'hello'
    return sol_from_main

Output:

hello

Yes

Explanation:

From the python doc, we can see next:

frame

f_back | next outer frame object (this frame’s caller)

f_locals | local namespace seen by this frame

So line A get the caller of current function, that is main function, and use f_locals to get all local variables of main function, so it can get the sol value from main. FYI in case you still need it.

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.