Variables in nested function don`t change their values after the first usage

I’m working on telegram bot, using telethon and aiogram. Code below is one of the methods of the Bot class. When it’s used twice, variables acc_phone_number, verification_code and client get new values, but the problem is that in get_acc_phone_number they have same values, as they had after the first usage of AddAccountForWork method.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>async def AddAccountForWork(self, callback: types.CallbackQuery):
@self.disp.message(F.text)
async def get_acc_phone_number(message: types.Message):
nonlocal acc_phone_number, verification_code, client, callback
print(acc_phone_number, verification_code, client, 'nn')
acc_phone_number = message.text
if self.db_manager.AccountForWorkExists(acc_phone_number):
await message.delete()
await callback.message.edit_text(messages.account_exists, reply_markup=keyboards.get_back_to_menu_keyboard())
else:
try:
await client.send_code_request(acc_phone_number)
time.sleep(0.3)
await message.delete()
await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())
except PhoneNumberInvalidError:
await message.delete()
await callback.message.edit_text(messages.wrong_phone)
@self.disp.callback_query(F.data.startswith('code_number'))
async def get_verification_code(callback: types.CallbackQuery):
nonlocal acc_phone_number, verification_code, client
symbol_num = 0
for symbol in verification_code:
if symbol == '_':
new_verification_code = list(verification_code)
new_verification_code[symbol_num] = callback.data[-1]
verification_code = ''
for i in new_verification_code: verification_code += i
break
symbol_num += 1
await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())
time.sleep(0.3)
if verification_code[-1] != '_':
try:
await client.sign_in(acc_phone_number, verification_code)
string_session = StringSession.save(client.session)
user_name = callback.from_user.username
self.db_manager.AddAccountForWork(user_name, string_session, acc_phone_number)
await client.log_out()
await client.disconnect()
client = None
await callback.message.edit_text(messages.finish_adding_account, reply_markup=keyboards.get_back_to_menu_keyboard())
except PhoneCodeInvalidError:
verification_code = '_____'
await callback.message.edit_text(messages.invalid_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())
acc_phone_number, verification_code = '', '_____'
tg_api_id, tg_api_hash = os.getenv('TGAPIID'), os.getenv('TGAPIHASH')
client = TelegramClient('session', tg_api_id, tg_api_hash, system_version='4.16.30-vxCUSTOM')
await client.connect()
await callback.message.edit_text(messages.ask_for_phone)
print(acc_phone_number, verification_code, client, 'nn')
</code>
<code>async def AddAccountForWork(self, callback: types.CallbackQuery): @self.disp.message(F.text) async def get_acc_phone_number(message: types.Message): nonlocal acc_phone_number, verification_code, client, callback print(acc_phone_number, verification_code, client, 'nn') acc_phone_number = message.text if self.db_manager.AccountForWorkExists(acc_phone_number): await message.delete() await callback.message.edit_text(messages.account_exists, reply_markup=keyboards.get_back_to_menu_keyboard()) else: try: await client.send_code_request(acc_phone_number) time.sleep(0.3) await message.delete() await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard()) except PhoneNumberInvalidError: await message.delete() await callback.message.edit_text(messages.wrong_phone) @self.disp.callback_query(F.data.startswith('code_number')) async def get_verification_code(callback: types.CallbackQuery): nonlocal acc_phone_number, verification_code, client symbol_num = 0 for symbol in verification_code: if symbol == '_': new_verification_code = list(verification_code) new_verification_code[symbol_num] = callback.data[-1] verification_code = '' for i in new_verification_code: verification_code += i break symbol_num += 1 await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard()) time.sleep(0.3) if verification_code[-1] != '_': try: await client.sign_in(acc_phone_number, verification_code) string_session = StringSession.save(client.session) user_name = callback.from_user.username self.db_manager.AddAccountForWork(user_name, string_session, acc_phone_number) await client.log_out() await client.disconnect() client = None await callback.message.edit_text(messages.finish_adding_account, reply_markup=keyboards.get_back_to_menu_keyboard()) except PhoneCodeInvalidError: verification_code = '_____' await callback.message.edit_text(messages.invalid_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard()) acc_phone_number, verification_code = '', '_____' tg_api_id, tg_api_hash = os.getenv('TGAPIID'), os.getenv('TGAPIHASH') client = TelegramClient('session', tg_api_id, tg_api_hash, system_version='4.16.30-vxCUSTOM') await client.connect() await callback.message.edit_text(messages.ask_for_phone) print(acc_phone_number, verification_code, client, 'nn') </code>
async def AddAccountForWork(self, callback: types.CallbackQuery):   
            
        @self.disp.message(F.text)
        async def get_acc_phone_number(message: types.Message):

            nonlocal acc_phone_number, verification_code, client, callback
            print(acc_phone_number, verification_code, client, 'nn')
            acc_phone_number = message.text
            if self.db_manager.AccountForWorkExists(acc_phone_number):
                await message.delete()
                await callback.message.edit_text(messages.account_exists, reply_markup=keyboards.get_back_to_menu_keyboard())
            else:
                try: 
                    await client.send_code_request(acc_phone_number)
                    time.sleep(0.3)
                    await message.delete()
                    await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())
                except PhoneNumberInvalidError:
                    await message.delete()
                    await callback.message.edit_text(messages.wrong_phone)


        @self.disp.callback_query(F.data.startswith('code_number'))
        async def get_verification_code(callback: types.CallbackQuery):

            nonlocal acc_phone_number, verification_code, client
            symbol_num = 0
            for symbol in verification_code:
                if symbol == '_': 
                    new_verification_code = list(verification_code)
                    new_verification_code[symbol_num] = callback.data[-1]
                    verification_code = ''
                    for i in new_verification_code: verification_code += i
                    break
                symbol_num += 1
            await callback.message.edit_text(messages.ask_for_verification_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())
            time.sleep(0.3)
            if verification_code[-1] != '_': 
                try:
                    await client.sign_in(acc_phone_number, verification_code)
                    string_session = StringSession.save(client.session)
                    user_name = callback.from_user.username
                    self.db_manager.AddAccountForWork(user_name, string_session, acc_phone_number)
                    await client.log_out()
                    await client.disconnect()
                    client = None
                    await callback.message.edit_text(messages.finish_adding_account, reply_markup=keyboards.get_back_to_menu_keyboard())
                except PhoneCodeInvalidError:
                    verification_code = '_____'
                    await callback.message.edit_text(messages.invalid_code.format(verification_code), reply_markup=keyboards.get_enter_verification_code_keyboard())


        acc_phone_number, verification_code = '', '_____'
        tg_api_id, tg_api_hash = os.getenv('TGAPIID'), os.getenv('TGAPIHASH')
        client = TelegramClient('session', tg_api_id, tg_api_hash, system_version='4.16.30-vxCUSTOM')
        await client.connect()
        await callback.message.edit_text(messages.ask_for_phone)
        print(acc_phone_number, verification_code, client, 'nn')

Can you please explain why?

New contributor

М Максаков is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật