On 13 October 2012 10:03, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Sat, Oct 13, 2012 at 7:41 PM, Thomas Bach<br>
<<a href="mailto:thbach@students.uni-mainz.de">thbach@students.uni-mainz.de</a>> wrote:<br>
> On Sat, Oct 13, 2012 at 12:32:41AM +0000, Steven D'Aprano wrote:<br>
>><br>
>> He gets SyntaxError because you can't follow a semicolon with a<br>
>> statement that begins a block.<br>
><br>
> Can someone provide a link on where to find this type of information?<br>
> I was just hunting through “The Python Language Reference” and could<br>
> not find anything explicit. The only thing I found is<br>
><br>
> <a href="http://docs.python.org/reference/simple_stmts.html" target="_blank">http://docs.python.org/reference/simple_stmts.html</a><br>
><br>
> “Several simple statements may occur on a single line separated by<br>
> semicolons.”<br>
><br>
> Anyways, this does not explicitly say “You shall not put a compound<br>
> statement after a simple statement separated by a semicolon.”, right?<br>
<br>
</div>It's more that Python treats simple and compound statements as<br>
completely separate beasts. You can combine simple statements on one<br>
line, but compound statements mustn't be.<br>
<br>
In my opinion, this is a major wart in Python syntax. You can argue<br>
all you like about how it reduces code clarity to put these sorts of<br>
things together, but that's a job for a style guide, NOT a language<br>
requirement. Most code won't put an assignment followed by an<br>
if/while/for, but when I'm working interactively, I often want to<br>
recall an entire statement to edit and reuse, complete with its<br>
initialization - something like (contrived example):<br>
<br>
>>> a=collections.defaultdict(int)<br>
>>> for x in open("RilvierRex.txt"): a[x]+=1<br>
<br>
Then I keep doing stuff, keep doing stuff, and then come back to this<br>
pair of lines. Since they're two lines, I have to recall them as two<br>
separate entities, rather than as an initializer and the code that<br>
uses it. Logically, they go together. Logically, they're on par with a<br>
list comprehension, which initializes, loops, and assigns, all as a<br>
single statement. But syntactically, they're two statements that have<br>
to go on separate lines.<br>
<br>
To force that sort of thing to be a single recallable statement, I can<br>
do stupid tricks like:<br>
<br>
>>> if True:<br>
a=collections.defaultdict(int)<br>
for x in open("RilvierRex.txt"): a[x]+=1<br>
<br>
but that only works in IDLE, not in command-line interactive Python.<br>
<br>
Note, by the way, that it's fine to put the statement _inside_ the for<br>
on the same line. It's even legal to have multiple such statements:<br>
<br>
>>> for x in (1,2,3): print(x); print(x);<br>
1<br>
1<br>
2<br>
2<br>
3<br>
3<br>
<br>
If there's any ambiguity, it would surely be that, and not the simple<br>
statement being first.<br>
<br>
Okay, rant over. I'll go back to being nice now. :)</blockquote><div><br></div><div>This here isn't a flaw in Python, though. It's a flaw in the command-line interpreter. By putting it all on one line, you are effectively saying: "group these". Which is the same as an "if True:" block, and some things like Reinteract even supply a grouping block like "build".</div>
<div><br></div><div>That said, because some shells suck it would be nice if:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace">python -c "a=1\nif a:print(a)"</font></blockquote><div>worked (just for -c).</div></div><br>