Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31190
| 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> |
|---|---|
| Date | 2012-10-13 20:03 +1100 |
| Subject | Re: How to use "while" within the command in -c option of python? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2110.1350119026.27098.python-list@python.org> (permalink) |
On Sat, Oct 13, 2012 at 7:41 PM, Thomas Bach
<thbach@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. :)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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