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

Joshua Landau joshua.landau.ws at gmail.com
Sat Oct 13 12:38:18 EDT 2012


On 13 October 2012 10:03, Chris Angelico <rosuav at gmail.com> wrote:

> On Sat, Oct 13, 2012 at 7:41 PM, Thomas Bach
> <thbach at 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. :)


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".

That said, because some shells suck it would be nice if:

> python -c "a=1\nif a:print(a)"

worked (just for -c).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121013/191e15fb/attachment.html>


More information about the Python-list mailing list