How to use "while" within the command in -c option of python?

Chris Angelico rosuav at gmail.com
Sat Oct 13 14:41:11 EDT 2012


On Sun, Oct 14, 2012 at 5:21 AM, Joshua Landau
<joshua.landau.ws at gmail.com> wrote:
> Because Python uses indentation, what would "if A: print(1); if B: print(2)"
> even do? It has 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)"].

sikorsky at sikorsky:~/cpython$ python -c "if False: print(1); print(2)"
sikorsky at sikorsky:~/cpython$ python -c "if True: print(1); print(2)"
1
2
sikorsky at sikorsky:~/cpython$

The semicolon separates statements, but doesn't change nesting levels.
The if statement increases the nesting level, which demands a
corresponding indentation increase if you go onto a new line; the
statement you describe is illegal because the 'if' isn't allowed to
not be at the beginning of the line, but it's unambiguous.

Of course, since Python lacks a non-whitespace way of indicating the
end of an if block, there's no way to put your "if A" and "if B" onto
the same line as peers. But that's a consequence of a design decision,
and one that can't easily be changed. Every language has warts like
that; eschewing variable declarations prevents infinite nesting of
scopes, but demanding variable declarations makes interactive work
harder. To paraphrase the Pirate King: Always follow the dictates of
your conscience, and accept the consequences.

ChrisA



More information about the Python-list mailing list