3 ms·
> The variable will still be bound, but conceptually it's unavailable, since it's not supposed to be used. Which, in all fairness, is the same as saying that t
by euccastro 16y ago
> The variable will still be bound, but conceptually it's unavailable, since it's not supposed to be used.
Which, in all fairness, is the same as saying that this does not indeed create a new scope. If you're going to rely on convention, you might as well dispense with the 'with' statement at all and use blank lines.
An explicit and safe (if not very elegant) way to put a variable out of scope is just deleting it after you're done with it:
main_scope_stuff()
if True: # Let's say we _really_ want indentation here..
c = create_context()
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
del c
# something_or_other(c) <--- raises UnboundLocalError
more_main_scope_stuff()
That said, if it got to the point of going to this trouble I'd just make a nested function instead.