开发者

Get variable from parent

I have two files. main.py that has main program logic and functions.py that has additional functions. Lets say main.py has this code

import functions
some_var = 'some value'

I would like to print out value of some_var in my functions.py fi开发者_Python百科le. How can I achieve this.


In general, you can do this my simply importing the main module within functions.py

Within your functions.py file:

import main
print main.some_var

However, you currently have a circular dependency problem. See Circular (or cyclic) imports in Python

You could put some_var into a third module, let's say constants.py and then main.py would look like:

import functions
from constants import some_var
... etc

and functions.py would be:

from constants import some_var

Solving your circular dependency problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜