从模块导入更新的变量
Import updated variable from module
我在更新模块后从模块导入变量时遇到问题。我正在处理 3 个文件:第一个是 'main' 文件并启动函数,第二个是 'setup' 并包含变量和修改它的函数,第三个是 'help' 使用变量的文件。问题是,当我启动 'main' 文件然后启动 'setup' 中的函数来修改变量时,一切正常。但是当我启动 'help' 文件时,它使用了未更新的变量,我不明白为什么。
main.py:
from colorama import init
from termcolor import colored
import subprocess
import check_functions_and_setup
init() # starts colorama
while True:
color = check_functions_and_setup.color_main
command = raw_input(colored("Insert function: \n >> ", color))
if (command == "help") or (command == "Help"):
process = subprocess.call('start /wait python help_function.py', shell=True)
elif command == "change color help":
# updates the color in the setup file
check_functions_and_setup.color_help = check_functions_and_setup.change_color_help(check_functions_and_setup.color_list, check_functions_and_setup.color_help, color)
设置:
color_help = "cyan"
color_list = ["grey", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]
def change_color_help(color_list, color_help, color):
input_color = raw_input(colored("Insert new color: \n >> ", color))
if input_color in color_list:
return input_color
else:
print colored("Invalid color", color)
return color_help
帮助:
from colorama import init
from termcolor import colored
import check_functions_and_setup
init() # starts colorama
print colored("WELCOME IN THE HELP PROGRAM!", check_functions_and_setup.color_help)
最后一行是无效的;事实上,即使我成功地从函数中更改了 help_color,该行也会以与原始 color_help 变量相同的颜色打印。
您是 运行 通过 subprocess
获得的帮助代码。该子进程不会与 main
为 运行 的父进程共享内存(以及您已更改颜色的位置)。与其 运行 将帮助代码作为一个单独的进程,不如 main
模块 import
帮助模块并调用您需要的任何功能。
这是您的代码的简化版本:
# main.py
import settings
from help import show_color
while True:
action = raw_input('Action: ')
if action == '':
break
elif action == 'help':
show_color()
else:
settings.set_color()
# settings.py
color = 'cyan'
def set_color():
global color
color = raw_input('Enter new color: ')
# help.py
import settings
def show_color():
print 'help.py: ', settings.color
请注意,您的设计——依赖于更改模块内全局变量的值——相当笨拙和僵化。几乎肯定有更好的方法来处理这种颜色变化。
我在更新模块后从模块导入变量时遇到问题。我正在处理 3 个文件:第一个是 'main' 文件并启动函数,第二个是 'setup' 并包含变量和修改它的函数,第三个是 'help' 使用变量的文件。问题是,当我启动 'main' 文件然后启动 'setup' 中的函数来修改变量时,一切正常。但是当我启动 'help' 文件时,它使用了未更新的变量,我不明白为什么。
main.py:
from colorama import init
from termcolor import colored
import subprocess
import check_functions_and_setup
init() # starts colorama
while True:
color = check_functions_and_setup.color_main
command = raw_input(colored("Insert function: \n >> ", color))
if (command == "help") or (command == "Help"):
process = subprocess.call('start /wait python help_function.py', shell=True)
elif command == "change color help":
# updates the color in the setup file
check_functions_and_setup.color_help = check_functions_and_setup.change_color_help(check_functions_and_setup.color_list, check_functions_and_setup.color_help, color)
设置:
color_help = "cyan"
color_list = ["grey", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]
def change_color_help(color_list, color_help, color):
input_color = raw_input(colored("Insert new color: \n >> ", color))
if input_color in color_list:
return input_color
else:
print colored("Invalid color", color)
return color_help
帮助:
from colorama import init
from termcolor import colored
import check_functions_and_setup
init() # starts colorama
print colored("WELCOME IN THE HELP PROGRAM!", check_functions_and_setup.color_help)
最后一行是无效的;事实上,即使我成功地从函数中更改了 help_color,该行也会以与原始 color_help 变量相同的颜色打印。
您是 运行 通过 subprocess
获得的帮助代码。该子进程不会与 main
为 运行 的父进程共享内存(以及您已更改颜色的位置)。与其 运行 将帮助代码作为一个单独的进程,不如 main
模块 import
帮助模块并调用您需要的任何功能。
这是您的代码的简化版本:
# main.py
import settings
from help import show_color
while True:
action = raw_input('Action: ')
if action == '':
break
elif action == 'help':
show_color()
else:
settings.set_color()
# settings.py
color = 'cyan'
def set_color():
global color
color = raw_input('Enter new color: ')
# help.py
import settings
def show_color():
print 'help.py: ', settings.color
请注意,您的设计——依赖于更改模块内全局变量的值——相当笨拙和僵化。几乎肯定有更好的方法来处理这种颜色变化。