T
T
Tribian2022-03-29 17:18:10
Python
Tribian, 2022-03-29 17:18:10

How to pass a variable between decorators?

The data in the variable is not updated when used in another decorator (I hope I phrased it correctly).

@dp.message_handler()
async def echo(message: types.Message, state: FSMContext):
    if message.text == 'запрос':
        # тут происходит запрос в БД и в переменную numbers выгружаются номера
         async with state.proxy() as take_user_data:
                take_user_data["numbers"] = all_numbers
         await Edit.waiting_Number.set()
         await bot.send_message(message.chat.id, 'Ok, напиши номер.')
         
         @dp.message_handler(state = Edit.waiting_Number)
         async def take(message: types.Message, state: FSMContext):
            async with state.proxy() as take_user_data:
                take_user_data["choose_number"] = message.text
             # сравниваем номер который ввел пользователь с номером из базы, если есть совпадение, то записываем его
             for res in take_user_data["numbers"]:
                 if int(take_user_data['choose_number']) == int(res):
                     catch += res


the first time the word "request" is used, for example, 10 numbers (from 1 to 10) are unloaded from the database, and if the user writes the number that is in take_user_data["numbers"] (from 1 to 10), then everything is ok.
But if after that one more number or several are added to the database (for example, up to 15. As a result, it will turn out from 1 to 15 in the database), then in the def echo function in take_user_data["numbers"] there will be all numbers from 1 to 15, and after transition by state Edit.waiting_Number in take_user_data["numbers"] there will be numbers from the very first call (from 1 to 10).
Tell me how to use the new data in take_user_data["numbers"] in the def take function.
I want to do without global

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question