发送变量到另一个脚本 Python

问题描述 投票:0回答:1

我如何将一个变量发送到另一个脚本?我有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"])

所以,我需要发送 dicmarkups.py

import bot

#to here
python python-3.x telegram-bot python-telegram-bot
1个回答
0
投票

我认为你需要做这样的事情。

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

-1
投票

你正在分配 dic 中的一个局部变量。language() 方法。

要解决这个问题,只需将 dic 在你 language() 这样的方法。

dic = {}

def language(message):
    global dic
    ...

你还需要把 global dic 的方法中,这样解释器就知道你正在改变全局变量 dic 在你的方法中

© www.soinside.com 2019 - 2024. All rights reserved.