0

I have 2 Python files: file1.py and file2.py. I want to import a variable from file1.py inside the file2.py but the variable is in a function. Here are the codes:

file1.py

class Ezber():
    def ask():
        value = "test string"

file2.py

from file1.py import *
print(value)

Is there any possibility of doing this?

2
  • 2
    No, you can't do that. Commented Aug 13, 2020 at 10:41
  • You dont have the variable 'value' in just another function, but a class method.. why not define the variable as a member of the class Ezber itself?? Commented Aug 13, 2020 at 10:48

3 Answers 3

3

No, you cannot access that information. The biggest reason is that ask() hasn't even been executed at the time of import. Therefor, the value variable hasn't been created.

More generally, however, unless you're writing something like a debugger or other diagnostic tool, you should not be trying to inspect the local variables of another block of code. If the ask() function needs to supply data to another module, it should do so through a return value or other documented way of sending information.

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

Comments

2

It is not possible to do what you're asking for, and it has nothing to do with the fact that your code resides in two different files. You would still have the same issue if you were trying to access a function's local variables inside the same file the function is declared.

The reason why you can't do this is that value is a local variable of function ask. This means that this variable will be pushed onto the stack whenever the function ask is called, and then removed once the execution of ask is over. So, it doesn't exist outside the scope of function ask's execution. You can understand why this happens by considering the scenario below:

def f(a):
   value = a + 2

Now, the value of variable value is not clear except for when the function f has been called. (f(2) would mean that value == 4, but f(3) would mean that value == 5).

But, if you have a variable whose value doesn't depend on any other arguments to the function, you can either declare it global (wouldn't recommend it), or make it a static variable like below:

class Ezber():
    def ask():
        # do something
        pass

Ezber.ask.value = "test string"

print(Ezber.ask.value)

Or, if you want the initialization to happen inside the function, you can do:

class Ezber():
    def ask():
        # do something
        Ezber.ask.value = "test string"

ezber = Ezber()
ezber.ask()

print(Ezber.ask.value)

But as you can see, you'll need to at least call ask once before you can access value. (since value is initialized only when ask is executed).

Comments

0

QUESTION SOLVED. I've done it by this way:

class Ezber:
    def ask(self):
        value = "test"
        return value
from file1 import *
print(Ezber().ask())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.