Danstiv / cm-assistant

1 stars 1 forks source link

Одноразовая переменная #23

Open TheZavitaev opened 2 years ago

TheZavitaev commented 2 years ago

https://github.com/Danstiv/cm-assistant/blob/master/tgbot/exception_handler.py#L114

TheZavitaev commented 2 years ago

Вообще, их там много :)

except pyrogram.errors.FloodWait as exception:

перезапишет твою переменную

Danstiv commented 2 years ago

А там ваще костыли какие-то, надо переделать :)

Danstiv commented 2 years ago

А, вот почему так. https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

except E as N:
    foo

was translated to

except E as N:
    try:
        foo
    finally:
        del N

This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.