Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31205

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'syntax': 0.03; 'languages,': 0.04; 'string.': 0.04; 'failing': 0.05; 'bash': 0.07; 'interpreter.': 0.07; 'newline': 0.07; 'see.': 0.07; 'subject:How': 0.09; 'python': 0.09; '"if': 0.09; 'command-line': 0.09; 'instance.': 0.09; 'newline,': 0.09; 'php,': 0.09; 'python:': 0.09; 'subject:command': 0.09; 'subject:while': 0.09; 'subject:python': 0.11; '(just': 0.16; 'backspace': 0.16; 'bash,': 0.16; 'enlighten': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'least,': 0.16; 'oct': 0.16; 'shells': 0.16; 'syntactic': 0.16; 'syntaxerror:': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'certainly': 0.17; 'expanded': 0.17; 'instance,': 0.17; 'shell': 0.18; 'putting': 0.20; '"",': 0.22; 'logical': 0.22; 'insert': 0.23; 'statement': 0.23; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'lines': 0.28; 'decide': 0.28; 'block,': 0.29; 'grouping': 0.29; 'leaves': 0.29; 'statements': 0.29; 'though.': 0.29; 'array': 0.29; "i'm": 0.29; 'worked': 0.30; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'cases,': 0.33; 'curious': 0.33; 'to:addr :python-list': 0.33; 'languages': 0.33; 'point.': 0.33; 'another': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'said,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'wanted': 0.36; 'subject:" ': 0.36; "didn't": 0.36; 'should': 0.36; 'unable': 0.36; 'option': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'side': 0.61; 'back': 0.62; 'here': 0.65; 'skip:$ 10': 0.66; 'guides': 0.71; 'challenge.': 0.84; 'desire,': 0.84; 'forced': 0.84; 'laid': 0.84; 'omission': 0.84; 'saying:': 0.84; 'subject: -': 0.84; 'retype': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=aD6upT0DKjtISCmQtJMNBDXFwPYamzrn0ES8SKAfUBI=; b=BP312x4w+YyjobX8STaNCUPHBqwuQQEZv6DNwcacocEI33Nvy5VhQErxJPyDne1i1n SfXSoOZDnauFyqUt+Cc5bc1nh/2VXytKYDQHvMgaBSa5cRGIS94c9pJHumXYztttIHxI rmWcRyafL6Jwvu8Jkv1MIR7h6QfPQGbLJDaiWXFjCnyK3Gx+f2+lUo2YLAq6Ki5V+z/v /Yju/oTqbF8x0DN71J2v9dT+HwOlF3+ZPeHQhmF/fyJOSm4i3efoLbwb+Y0M8plEw8Qq L/lMD9RTp9WGH6RBWggTNvMbiTb8UJuLLT1WAT+alnYL7tRL073xKdsw2RpSbIomAbeg QDQA==
MIME-Version 1.0
In-Reply-To <CAN1F8qX50y6uVFXs6OxZaVYyUPT-1jLHqY5GoDr3x4Xctd0Xtw@mail.gmail.com>
References <CANOe_mhhn31Qb2iB1nWydWSm=MJ0xK0o5vxWR+mLeO5ggqwmwQ@mail.gmail.com> <mailman.2101.1350083051.27098.python-list@python.org> <5078b6a9$0$6574$c3e8da3$5496439d@news.astraweb.com> <20121013084132.GA5083@taris.box> <CAPTjJmrBc1hABysXXpkTs3YWnBukEg=WiZD2TFnUWnQmXJ-t7g@mail.gmail.com> <CAN1F8qX50y6uVFXs6OxZaVYyUPT-1jLHqY5GoDr3x4Xctd0Xtw@mail.gmail.com>
Date Sun, 14 Oct 2012 04:23:25 +1100
Subject Re: How to use "while" within the command in -c option of python?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2125.1350149008.27098.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350149008 news.xs4all.nl 6893 [2001:888:2000:d::a6]:48211
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31205

Show key headers only | View raw


On Sun, Oct 14, 2012 at 3:38 AM, Joshua Landau
<joshua.landau.ws@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).

Yes, that'd be nice. But it still leaves the big question of why
Python requires \n to separate one statement from another. It IS a
flaw in Python that it requires one specific statement separator in
this instance, even though it'll accept two in another instance.

Here's a side challenge. In any shell you like, start with this
failing statement, and then fix it without retyping anything:

sikorsky@sikorsky:~$ python -c "a=1; if a: print(a)"
  File "<string>", line 1
    a=1; if a: print(a)
          ^
SyntaxError: invalid syntax

In bash, I was unable to insert a newline into the quoted string. My
only option was to backspace everything after the point where I wanted
the newline, then hit enter, then retype the if. I'm curious to know
if that's simply because I didn't think of (some bash feature), or
alternatively, if there's another shell that would have made this
easy.

Back to the main point. In C-like languages, the newline is nothing
special. (ECMAScript allows the omission of semicolons at end of line
in many cases, but many style guides recommend using them anyway.) You
can, if you so desire, put all your code into a single line. It's then
up to the project's style guide to decide how things should be laid
out. For instance, this is multiple statements in PHP, but I see it as
one logical action:

$bar=array(); for ($foo as $k=>$v) $bar[$k]="<p>".$v."</p>";

It's one statement in Python:

bar = ["<p>"+x+"</p>" for x in foo]

It's one statement in Pike:

array bar = map(foo,lambda(string x) {return "<p>"+x+"</p>";});

So it should be allowed to be put on one line. And in languages whose
syntax derives from C, you almost certainly can. (I can't think of any
counter-examples, though that certainly doesn't prove they don't
exist.) But the same thing is forced onto two lines in Python, and not
for syntactic reasons - at least, not that I can see. Perhaps someone
can enlighten me.

Is there any fundamental reason that the syntax couldn't be expanded
to permit "statement; statement" for any two given statements?

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: How to use "while" within the command in -c option of python? Etienne Robillard <animelovin@gmail.com> - 2012-10-12 19:04 -0400
  Re: How to use "while" within the command in -c option of python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-13 00:32 +0000
    Re: How to use "while" within the command in -c option of python? Thomas Bach <thbach@students.uni-mainz.de> - 2012-10-13 10:41 +0200
    Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-13 20:03 +1100
    Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-14 04:23 +1100
      Re: How to use "while" within the command in -c option of python? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-13 20:43 +0300
        Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-14 04:47 +1100
    Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-14 05:41 +1100
    Re: How to use "while" within the command in -c option of python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-13 15:19 -0400
    Re: How to use "while" within the command in -c option of python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-13 16:42 -0400
    Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-14 08:39 +1100
    Re: How to use "while" within the command in -c option of python? Chris Angelico <rosuav@gmail.com> - 2012-10-14 09:09 +1100

csiph-web