On Sat, Oct 13, 2012 at 7:41 PM, Thomas Bach <thb...@students.uni-mainz.de> wrote: > On Sat, Oct 13, 2012 at 12:32:41AM +0000, Steven D'Aprano wrote: >> >> He gets SyntaxError because you can't follow a semicolon with a >> statement that begins a block. > > Can someone provide a link on where to find this type of information? > I was just hunting through “The Python Language Reference” and could > not find anything explicit. The only thing I found is > > http://docs.python.org/reference/simple_stmts.html > > “Several simple statements may occur on a single line separated by > semicolons.” > > Anyways, this does not explicitly say “You shall not put a compound > statement after a simple statement separated by a semicolon.”, right?
It's more that Python treats simple and compound statements as completely separate beasts. You can combine simple statements on one line, but compound statements mustn't be. In my opinion, this is a major wart in Python syntax. You can argue all you like about how it reduces code clarity to put these sorts of things together, but that's a job for a style guide, NOT a language requirement. Most code won't put an assignment followed by an if/while/for, but when I'm working interactively, I often want to recall an entire statement to edit and reuse, complete with its initialization - something like (contrived example): >>> a=collections.defaultdict(int) >>> for x in open("RilvierRex.txt"): a[x]+=1 Then I keep doing stuff, keep doing stuff, and then come back to this pair of lines. Since they're two lines, I have to recall them as two separate entities, rather than as an initializer and the code that uses it. Logically, they go together. Logically, they're on par with a list comprehension, which initializes, loops, and assigns, all as a single statement. But syntactically, they're two statements that have to go on separate lines. To force that sort of thing to be a single recallable statement, I can do stupid tricks like: >>> if True: a=collections.defaultdict(int) for x in open("RilvierRex.txt"): a[x]+=1 but that only works in IDLE, not in command-line interactive Python. Note, by the way, that it's fine to put the statement _inside_ the for on the same line. It's even legal to have multiple such statements: >>> for x in (1,2,3): print(x); print(x); 1 1 2 2 3 3 If there's any ambiguity, it would surely be that, and not the simple statement being first. Okay, rant over. I'll go back to being nice now. :) ChrisA -- http://mail.python.org/mailman/listinfo/python-list