如何在两个 Python 文件之间发送动态变量?
How to send a dynamic variable between two Python files?
我正在尝试将一个变量从 graphics.py
传递到 core.py
。
graphics.py
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
#some code....
global w
w = cross.xcor()
core.py
import turtle
import graphics
print(graphics.w)
这个变量(graphics.py
中的w
每次我单击鼠标按钮时都会改变它的值(不是问题的一部分,但你最好知道它)。
当我执行它时,它只传递一次变量,并且不会在变量值发生变化时再次传递该变量。它传递给 core.py
的值是 0
。(仅一次)
我如何确保每次它的值发生变化时我都能访问这个变量(从 core.py
)并且它不会保留 0
graphics.py
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
#some code....
w = cross.xcor()
return w
core.py
import turtle
import graphics
print(graphics.xcross(0, 1))
在我看来,变量应该限制在每个 python 文件中。如果您需要共享它们,请添加一个 getter 方法以从其他文件中检索值。
考虑到您的示例,我在 graphics.py
文件上添加了一个 get_w()
方法,用于 core.py
文件。
graphics.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
# Some code...
# W modification
global w
w = 1
def xcross2(x,y):
# Some code...
# W modification
global w
w = 2
def get_w():
return w
core.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# For better print formatting
from __future__ import print_function
# Imports
import turtle
from graphics import get_w
# Code
def core_func():
print(get_w())
例子main.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import graphics
import core
# Code
def main():
graphics.xcross(1, 2)
core.core_func()
graphics.xcross2(1, 2)
core.core_func()
if __name__ == "__main__":
main()
运行:
python main.py
输出:
1
2
请注意,core.py
代码现在位于一个函数内,以便能够从 main.py
中调用它,但您可以像示例中那样保留它。另外,我已经复制了 xcross
方法以显示如何修改 w
值,您仍然可以从 core.py
文件中获取它。在您的实际应用程序中,您可以保留一个 xcross
方法并在需要时进行 w
修改。
我正在尝试将一个变量从 graphics.py
传递到 core.py
。
graphics.py
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
#some code....
global w
w = cross.xcor()
core.py
import turtle
import graphics
print(graphics.w)
这个变量(graphics.py
中的w
每次我单击鼠标按钮时都会改变它的值(不是问题的一部分,但你最好知道它)。
当我执行它时,它只传递一次变量,并且不会在变量值发生变化时再次传递该变量。它传递给 core.py
的值是 0
。(仅一次)
我如何确保每次它的值发生变化时我都能访问这个变量(从 core.py
)并且它不会保留 0
graphics.py
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
#some code....
w = cross.xcor()
return w
core.py
import turtle
import graphics
print(graphics.xcross(0, 1))
在我看来,变量应该限制在每个 python 文件中。如果您需要共享它们,请添加一个 getter 方法以从其他文件中检索值。
考虑到您的示例,我在 graphics.py
文件上添加了一个 get_w()
方法,用于 core.py
文件。
graphics.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import turtle
from turtle import Turtle, Screen
def xcross(x, y):
cross = turtle.Turtle()
# Some code...
# W modification
global w
w = 1
def xcross2(x,y):
# Some code...
# W modification
global w
w = 2
def get_w():
return w
core.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# For better print formatting
from __future__ import print_function
# Imports
import turtle
from graphics import get_w
# Code
def core_func():
print(get_w())
例子main.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import graphics
import core
# Code
def main():
graphics.xcross(1, 2)
core.core_func()
graphics.xcross2(1, 2)
core.core_func()
if __name__ == "__main__":
main()
运行:
python main.py
输出:
1
2
请注意,core.py
代码现在位于一个函数内,以便能够从 main.py
中调用它,但您可以像示例中那样保留它。另外,我已经复制了 xcross
方法以显示如何修改 w
值,您仍然可以从 core.py
文件中获取它。在您的实际应用程序中,您可以保留一个 xcross
方法并在需要时进行 w
修改。