On 13 October 2012 18:23, 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 Sun, Oct 14, 2012 at 3:38 AM, Joshua Landau<br>
<<a href="mailto:joshua.landau.ws@gmail.com">joshua.landau.ws@gmail.com</a>> wrote:<br>
> This here isn't a flaw in Python, though. It's a flaw in the command-line<br>
> interpreter. By putting it all on one line, you are effectively saying:<br>
> "group these". Which is the same as an "if True:" block, and some things<br>
> like Reinteract even supply a grouping block like "build".<br>
><br>
> That said, because some shells suck it would be nice if:<br>
>><br>
>> python -c "a=1\nif a:print(a)"<br>
><br>
> worked (just for -c).<br>
<br>
</div>Yes, that'd be nice. But it still leaves the big question of why<br>
Python requires \n to separate one statement from another. It IS a<br>
flaw in Python that it requires one specific statement separator in<br>
this instance, even though it'll accept two in another instance.<br></blockquote><div><br></div><div>Not really. The two things are fundamentally different. Yes, it's hard to think of another example, but they aren't the same. ";" means "implicit newline to the <i>same indentation</i>", whereas a newline does not.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Here's a side challenge. In any shell you like, start with this<br>
failing statement, and then fix it without retyping anything:<br>
<br>
sikorsky@sikorsky:~$ python -c "a=1; if a: print(a)"<br>
<div class="im"> File "<string>", line 1<br>
</div> a=1; if a: print(a)<br>
^<br>
SyntaxError: invalid syntax<br>
<br>
In bash, I was unable to insert a newline into the quoted string. My<br>
only option was to backspace everything after the point where I wanted<br>
the newline, then hit enter, then retype the if. I'm curious to know<br>
if that's simply because I didn't think of (some bash feature), or<br>
alternatively, if there's another shell that would have made this<br>
easy.<br></blockquote><div><br></div><div>I have to thank Jussi Piitulainen for the command here, I'm likely gonna' use that quite a bit.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Back to the main point. In C-like languages, the newline is nothing<br>
special. (ECMAScript allows the omission of semicolons at end of line<br>
in many cases, but many style guides recommend using them anyway.) You<br>
can, if you so desire, put all your code into a single line. It's then<br>
up to the project's style guide to decide how things should be laid<br>
out. For instance, this is multiple statements in PHP, but I see it as<br>
one logical action:<br>
<br>
$bar=array(); for ($foo as $k=>$v) $bar[$k]="<p>".$v."</p>";<br>
<br>
It's one statement in Python:<br>
<br>
bar = ["<p>"+x+"</p>" for x in foo]<br>
<br>
It's one statement in Pike:<br>
<br>
array bar = map(foo,lambda(string x) {return "<p>"+x+"</p>";});<br>
<br>
So it should be allowed to be put on one line</blockquote><div><br></div><div>Your argument goes:<br></div><div>FACT 1: You can do this in one statement, in one line.</div><div>FACT 2: In other languages you can do this in two statements, in one line</div>
<div>CONCULSION: You should be able to do this in two statements, in one line</div><div><br></div><div>Which I don't agree with. I understand that it is one *logical action*, but so is multiplying a matrix. We have functions to <i>wrap</i> logical actions that are composed of multiple logical actions.</div>
<div><br></div><div>The problem is that Python uses indentation. That's fundamentally the problem.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
And in languages whose<br>
syntax derives from C, you almost certainly can. (I can't think of any<br>
counter-examples, though that certainly doesn't prove they don't<br>
exist.) But the same thing is forced onto two lines in Python, and not<br>
for syntactic reasons - at least, not that I can see. Perhaps someone<br>
can enlighten me.<br>
<br>
Is there any fundamental reason that the syntax couldn't be expanded<br>
to permit "statement; statement" for any two given statements?<span class="HOEnZb"><font color="#888888"><br>
</font></span></blockquote></div><br><div>Because Python uses indentation, what would "if A: print(1); if B: print(2)" even do? It <i>has</i> to fail, because we have to assume consistent indentation for ";"s*. With "\n" as I proposed, you still have to indent: it is just a method to bypass lame shells [it would become "if A: print(1)\nif B: print(2)"].</div>
<div><br></div><div>Blocks that avoid this have been done to death with multi-line lambdas, and the majority of people don't like them. I'm sway-able, but it's not going to happen because Guido and most of the peeps with power aren't. Anything that challenges the standard now is dead from the start.</div>
<div><br></div><div>* You <i>could</i> have some rules that govern ambiguities well, but none that I can think of would be <i>both</i> backward-compatible <i>and</i> complete. If it's incomplete we've just moved the goalpost.</div>