with statements don't create scope
I saw some code today that initialized a variable inside with and continued to use it outside the block.
I’ve always assumed with statements had their own scope so this had me a little confused. Turns out this is perfectly fine.
A
withstatement does not create a scope (likeif,forandwhiledo not create a scope either).As a result, Python will analyze the code and see that you made an assignment in the
withstatement, and thus that will make the variable local (to the real scope).In Python variables do not need initialization in all code paths: as a programmer, you are responsible to make sure that a variable is assigned before it is used. This can result in shorter code: say for instance you know for sure that a list contains at least one element, then you can assign in a
forloop. In Java assignment in aforloop is not considered safe (since it is possible that the body of the loop is never executed).Initialization before the
withscope can be safer in the sense that after thewithstatement we can safely assume that the variable exists. If on the other hand the variable should be assigned in thewithstatement, not initializing it before thewithstatement actually results in an additional check: Python will error if somehow the assignment was skipped in thewithstatement.A
withstatement is only used for context management purposes. It forces (by syntax) that the context you open in thewithis closed at the end of the indentation.