Using a variable in a try,catch,finally statement without declaring it outside

前端 未结 2 597
执念已碎
执念已碎 2020-12-25 11:07

I\'m pretty new to Python, here is some code I am looking at:

try:
    connection = getConnection(database)
    cursor = connection.cursor()
    cursor.execu         


        
2条回答
  •  無奈伤痛
    2020-12-25 11:57

    Python does not have block scope. Anything defined inside the try block will be available outside.

    That said, you would still have a problem: if it is the getConnection() call that raises the error, cursor will be undefined, so the reference in the finally block will error.

提交回复
热议问题