将变量发送到另一个脚本Python

2024-05-16 18:42:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将一个变量发送到另一个脚本?我有3个脚本(bot.pylanguage_dict.pymarkups.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

Tags: pyimport脚本idmessage字典botchat
2条回答

我想你需要做这样的事情

#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()

language()方法中将dic指定为局部变量

要解决此问题,只需在language()方法上方指定dic,如下所示:

dic = {}

def language(message):
    global dic
    ...

您还需要在方法中放入global dic,以便解释器知道您正在更改方法中的全局变量dic

相关问题 更多 >