我如何将一个变量发送到另一个脚本?我有3个脚本(bot.py
, language_dict.py
, markups.py
)
language_dict.py
词典
russian = {
"section": "Выберите раздел ➡",
"individual": "Физические лица",
"legal_entity": "Юридические лица",
}
在 bot.py
我宣布这本字典是 dic
可变的
import language_dict
def language(message):
chat_id = message.chat.id
if message.text == "Русский":
dic = language_dict.russian
msg = bot.send_message(chat_id, dictionary["section"])
所以,我需要发送 dic
到 markups.py
import bot
#to here
我认为你需要做这样的事情。
#in bot.py
def class1():
def func1(self):
#do something
#dic = language_dict.russian
return #dic
#in markups.py
#from bot.py import class1
#create object of class1 under main method
class1_obj = class1()
dic = class1.func1()
你正在分配 dic
中的一个局部变量。language()
方法。
要解决这个问题,只需将 dic
在你 language()
这样的方法。
dic = {}
def language(message):
global dic
...
你还需要把 global dic
的方法中,这样解释器就知道你正在改变全局变量 dic
在你的方法中