Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Python does actually have a single instance of sub-function scopes: When you say `try: ... except Exception as e: ...` the `e` variable is deleted at the end of the `except` clause. I think this is because the exception object, via the traceback, refers to the handling function's invocation record, which in turn contains a map of all the function's local variables. So if the variable worked like normal variables in Python it'd create a reference cycle and make the Python GC sad. So if you need that behaviour, you need to reassign the exception to a new name [0].

0: https://docs.python.org/3/reference/compound_stmts.html#the-...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: