Multi-line commands with 'python -c'

Chris Angelico rosuav at gmail.com
Fri May 30 08:58:18 EDT 2014


On Fri, May 30, 2014 at 10:47 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> On Friday, May 30, 2014 12:50:31 PM UTC+5:30, Chris Angelico wrote:
>> On Fri, May 30, 2014 at 4:04 PM, Rustom Mody wrote:
>> > I thought when one signs up for python one has to sign an affidavit
>> > saying:
>> > "I shall not write one-liners\n" * 100
>
>> Certainly not. I write all my list comps on one line!
>
>> *ducking for cover*
>
> Heres a more vile misuse of python3's print-as-function + list-comp-as-for:
>
> python3 -c 'from os import walk; [print(r) for r, d, f in walk(".") if len(d+f) == 1]'
>
> Well if C programmers can use ',' as one-line ';' and '?:' as one-line if
> why not python also?
>
> [To noobs who are reading: Dont do this!]

I actually crafted the exact same vile misuse, prior to asking the question.

https://lists.debian.org/debian-user/2014/05/msg02019.html

Modulo trivialities like whitespace and the from-import, it's exactly
the same as your version.

Incidentally, C's ?: operator maps to Python's ternary if/else
operator, which most definitely is valid in a one-liner. So it's just
the semicolon that you're looking at. In C, you can combine any two
statements onto one line; in Python, certain statements may not follow
a semicolon. So it's not really ; and ?: that are the point here, but
that Python, with its stricter rules about newlines (as opposed to
"any whitespace"), doesn't seem to have a convenient notation for
putting multiple lines into a -c command.

ChrisA



More information about the Python-list mailing list