Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'explicitly': 0.04; 'lines,': 0.05; 'lines.': 0.07; 'subject:How': 0.09; 'python': 0.09; 'block.': 0.09; 'command-line': 0.09; 'information?': 0.09; 'subject:command': 0.09; 'subject:while': 0.09; 'subject:python': 0.11; 'language': 0.14; 'sat,': 0.15; 'anyways,': 0.16; 'assigns,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idle,': 0.16; 'oct': 0.16; 'semicolon': 0.16; 'sorts': 0.16; 'statement.': 0.16; 'stuff,': 0.16; 'syntax.': 0.16; 'true:': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'followed': 0.20; 'sort': 0.21; 'received:209.85.214.174': 0.21; 'together.': 0.21; 'assignment': 0.22; 'statement': 0.23; 'header :In-Reply-To:1': 0.25; 'separate': 0.27; 'first.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'fine': 0.28; 'argue': 0.29; "d'aprano": 0.29; 'separated': 0.29; 'statements': 0.29; 'steven': 0.29; "i'm": 0.29; 'code': 0.31; 'gets': 0.32; 'url:python': 0.32; 'could': 0.32; 'like:': 0.33; 'requirement.': 0.33; 'right?': 0.33; 'surely': 0.33; 'to:addr:python-list': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'subject:?': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'be.': 0.36; 'subject:" ': 0.36; 'anything': 0.36; "i'll": 0.36; 'does': 0.37; 'two': 0.37; 'uses': 0.37; 'being': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'things': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'skip:a 30': 0.60; 'link': 0.60; 'most': 0.61; 'back': 0.62; 'thomas': 0.62; 'provide': 0.62; 'more': 0.63; 'charset:windows-1252': 0.65; 'legal': 0.65; 'tricks': 0.65; 'entities,': 0.84; 'reference\x94': 0.84; 'subject: -': 0.84; 'together,': 0.84; 'treats': 0.84; 'url:reference': 0.84; '\x93the': 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:content-transfer-encoding; bh=Z6hJSEH2XAHhMNGj+HAUyZyzsQ0o+Px0aP2JcG2Kpl8=; b=ChHHor9Hr+h3T+nGdjeW0MWURE55oZ8u424oeqKICQuvKLgwDoEbtjNVSelmIbJ2uO nEgNKBt7kzQiw/4zG+eZi/jEpD0H26LYxyu9DOT5PMo36gwiwNHNvpwgz1qvX4GDIX4O ACZwzLPUDoE2a6X/myAM03WBB4kQP1EKedfupnnoLuS+x0D+Z0xrIUAcy3zxqwNbbqKi AelAR6n0m4mLSUHNmB+njIXJNfjBWF1bsvkivPERsHp9UMv6rC8zayeGz0TCkcod6tBO 0sCBNREVDU/XFjjqLmtRXV0Shdk3Cmqi92XCZWJp0cklKJ20+1fCz3ebq4fwQZzgev7J qxSA== MIME-Version: 1.0 In-Reply-To: <20121013084132.GA5083@taris.box> References: <5078b6a9$0$6574$c3e8da3$5496439d@news.astraweb.com> <20121013084132.GA5083@taris.box> Date: Sat, 13 Oct 2012 20:03:42 +1100 Subject: Re: How to use "while" within the command in -c option of python? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350119026 news.xs4all.nl 6960 [2001:888:2000:d::a6]:39901 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31190 On Sat, Oct 13, 2012 at 7:41 PM, Thomas Bach 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 =93The Python Language Reference=94 and could > not find anything explicit. The only thing I found is > > http://docs.python.org/reference/simple_stmts.html > > =93Several simple statements may occur on a single line separated by > semicolons.=94 > > Anyways, this does not explicitly say =93You shall not put a compound > statement after a simple statement separated by a semicolon.=94, 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=3Dcollections.defaultdict(int) >>> for x in open("RilvierRex.txt"): a[x]+=3D1 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=3Dcollections.defaultdict(int) for x in open("RilvierRex.txt"): a[x]+=3D1 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. :) ChrisA