if 1 < 2:
c = 2
else:
c = 3
print c
(I'm retarded and don't know how to do comments that have new lines in the, but hopefully you catch the drift. The 'print c' is at 0 indentation)What you try to do is not legal Python. The else clause can't go in the same line.
I get your point, though. Python's scope is not in terms of literally 'indentation' blocks. Scope units are: module toplevel, and (possibly nested) function and class definitions. To create a new scope you create a new function. This is what Lisp does too (you can define let as a macro over lambda).
I have oscillated between liking either way better and at present I find Python's sloppier way more practical.
x = 2
f = lambda : x
print f()
x = 3
print f() main_scope_stuff()
c = create_context()
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
more_main_scope_stuff()
to emphasize that c was only important to the loop and the call to "accumulate"? main_scope_stuff()
with create_context() as c:
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
more_main_scope_stuff()
That signifies that "c" isn't supposed to be available after scope of the "with" statement. The variable will still be bound, but conceptually it's unavailable, since it's not supposed to be used.Also, the object in create_context() needs to support being called like that (i.e. it should contain __enter__ and __exit__ methods).
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.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.
I'm not really sure what you're trying to show with that code, though. Is your objection that every do_ function is requiring a context?
Edit: I see what the parent was saying now, see bottom comment. Also, see StavrosK's comment if you're fine using Python 2.5 (released 2006).
The code I wrote does this (because c is 1 indentation level deeper than the main_scope functions). I do not know if it is legal Python to simply increase indentation level to demarcate a new scope, but I suspect it is not, and it is certainly not common practice.
This is really a minor gripe - but then again so are the article's - it's pretty easy to tell a binding from a function call.
c = create_context()
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
do_other_stuff
One could also throw the separate stuff into a new function.That's the elephant in the room in all of these "my language is more readable" discussions, isn't it?
I actually find that Lisp scores some local-readability points over Python in a few cases, mainly because Lisp is more explicit about scope, e.g.
versus is more explicit about the intended lifetimes and consumers of the data "a" and "b". Lisp also has the "with a = (blah)" keyword for "loop" so the assignment can be made specifically in the context of loop preparation.All in all I'm actually happier with my ability to signal my intent (and gauge other programmers' intent) in Lisp than I am in Python.