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

Joshua Landau joshua.landau.ws at gmail.com
Mon Oct 15 17:24:08 EDT 2012


On 13 October 2012 17:38, Joshua Landau <joshua.landau.ws at gmail.com> wrote:

> 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).
>
>
And, spurred on by another thread, this has become possible.

Make a file like so, install it if you wish.

import sys
> evaluable = sys.argv[1].encode("utf8").decode("unicode_escape")
> exec(evaluable)


This lets you do this:

python unicode_escape.py "a = []    ##\nfor x in range(100):    ##\n
>  a.append(x)    ##\n  a.append(x**2)    ##\nprint(a)"


It's ugly, but it works and only takes one line.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121015/4e5e238e/attachment.html>


More information about the Python-list mailing list