> 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.
Re:Lisp vs Python
by neutronicus on Sunday February 20, 2011 @08:07PM
(#2243779)
That's nice, I didn't know about that, thanks. I'll definitely be using it the next time I have to use Python.
It's a little bit unsatisfying, since all of the context has to be wrapped in a single object, but that does cover a common class of Lisp macro use cases.
Also, the object in create_context() needs to support being called like that (i.e. it should contain __enter__ and __exit__ methods).