我有一個由多個腳本引用的幫助函數。我的想法是,許多腳本可以使用助手函數基本上附加到列表,然後在某些時候,這些腳本中的任何一個都可以訪問列表中的元素。跨腳本存儲變量
某些腳本使用傳統的「導入」語句,而其他腳本使用「importlib.import_module()」,並且這兩者的組合似乎會導致問題。
我遇到的問題是輔助函數只存儲當前腳本寫入的值。
舉例來說,如果我有:
Project/
|-- helpers/
| |--store_value.py
|-- framework/
| |runner/
| | |-- runner.py
| |validator/
| | |-- validator.py
而且在store_value.py如下:
ALL_MSGS = []
def print_message(msg, display_messages=True):
"""
:param msg: [str] - String to be printed
:return: None - Work In Progress
"""
global ALL_MSGS
ALL_MSGS.append(msg)
if display_messages:
print(msg)
def return_all_msgs():
return ALL_MSGS
在Runner.py我:
import store_value, validator
...
store_value.print_message("Calling Validator")
validator.validate()
print(store_value.return_all_msgs())
...
和確認。 py:
importlib.import_module("store_value")
...
store_value.print_message("Running Validator")
...
然後我希望可以將輸出爲:
["Calling Validator", "Running Validator"]
但現在我越來越:
[]
你是如何運行腳本的? –