Home > Net >  transfer the value from def() to another def()
transfer the value from def() to another def()

Time:11-24

How can I transfer the value of the mes variable from one function to another?

def forwardmes2withdelay(message):
    print(message.text)
    if message.text == 'Главное меню':
        button = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=1)
        contacts = types.KeyboardButton('Контакты')
        post = types.KeyboardButton('Разместить пост')
        button.add(contacts, post)
        bot.send_message(message.chat.id, 'Главное меню', parse_mode='')
        bot.send_message(message.chat.id, 'Выберите действие', reply_markup=button)
    else:
        try:
            mes = message.id
            return mes
        finally:
            mesg = bot.send_message(message.chat.id,'Укажите желаемое вами время формата <i>ЧЧ:СС</i>', parse_mode='HTML')
            bot.register_next_step_handler(mesg, forwardmestime)



def forwardmestime(message):
    print(mes)
    timeobj = datetime.strptime(message.text, '%H:%M').time()
    if f'{currentdatetime.hour}:{currentdatetime.minute}' == f'{timeobj.hour}:{timeobj.minute}':
        bot.copy_message(chat2, message.chat.id, mes)
    else:
        def tusk():
            bot.copy_message(chat2, message.chat.id, mes)
            return schedule.CancelJob
        schedule.every().day.at(f'{timeobj}').do(tusk)
        while True:
            schedule.run_pending()
            time.sleep(1)

I want the mes value from the first function to go to another

CodePudding user response:

First, you need to have your def functions inside the class.

Second. you need to have your variables declared inside that class.

This way you can change that variable in one function and then use it in another function. Just like the example below.

To access it you need to use "self."

class Person():
    def __init__(self):
        self._name = None

    def changeNameToJames(self):
        self._name = "James"

    def nameIs(self):
        print(self._name)
  • Related