Python indentation

D

David Fraser

Antoon said:
Yes, and I would also be interested in an other proposal
that went something like:

while condition1:
code
and while condition2:
code
and while condition3:
code

maybe we should combine them into something like

do:
code
and while condition1:
code
and while condition3:
code

The trouble like this is I can't see what it means... it actually reads
like concurrent execution - 'do this and this and this...'
So I don't think its very readable...

David
 
D

David Fraser

Antoon said:
Well that would of course be the best solution. But in absence
of a more general loop construction I'm willing to settle for
a construction that looks like it, even if it has to be
simulated.

The thing is, the readability is lost if what it looks like is different
to what it actually does. Anyway, you can't do that using Python because
of the indentation of the language so if you want to you'll have to use
another language :)
Maybe you are just not familiar with more general loop constructs.

What the algorithm is trying to do is find the least significant bit
that is on. It does so by applying succeeding masks to the number
each mask is all ones and double in length as the previous mask.
when applying a mask results in a number different from 0, the
number is shifted the length of the previous mask and the length
is added to the offset. The process is then repeated with the
mask initialized at 1. The algorthm stops when applying a mask
of 1 already differs from 0.

Sure, but I didn't find the loop structure itself readable. Maybe just a
matter of what some people like others don't.
 
P

Peter Hansen

Antoon said:
Yes, and I would also be interested in an other proposal
that went something like:


while condition1:
code
and while condition2:
code
and while condition3:
code

maybe we should combine them into something like

do:
code
and while condition1:
code
and while condition3:
code

As with David, I have no idea what these are supposed to do.
Could you please translate them into the existing Python
loop idiom (while,if/break etc) so that those of us who can't
read minds can understand what the above constructs are
intended to do?

-Peter
 
C

Christopher T King

In Vim:
set sw=4 sta et ai bs=2

Put that in your .vimrc, and you're done. The key labelled Tab is now
just a convenient way of specifying "indent the standard four spaces",
and backspace on leading spaces removes the last four. Done.

I dunno... that looks like some kind of voodoo chant to me... ;)
 
P

Paramjit Oberoi

Yes, and I would also be interested in an other proposal
Andrew Koenig proposed this in the following email:

http://groups.google.com/[email protected]

If link doesn't work - it was a post dated 2003-05-05 with the subject
"Re: PEP 315: Enhanced While Loop". There was a fairly long thread about
it back then.
 
D

David Fraser

Paramjit said:
Andrew Koenig proposed this in the following email:

http://groups.google.com/[email protected]

If link doesn't work - it was a post dated 2003-05-05 with the subject
"Re: PEP 315: Enhanced While Loop". There was a fairly long thread about
it back then.

A quote from the message:
while <condition1>:
<code1>
and while <condition2>:
<code2>
and while <condition3>:
<code3>

[snip] would be equivalent to

while <condition1>:
<code1>
if not (<condition2>): break
<code2>
if not (<condition3>): break
<code3>

The thing is, to me as an English speaker, there is no logical
correlation between the proposed syntax and its semantics.
For example, why shouldn't code3 be executed if condition2 is false and
condition3 is true?

David
 
J

Josef Meile

Well, one could apply another coding style in this example:
if (condition) {
doThis();
doThat();
} else {
doWhatever();
andSoOn();
}
I don't like that style. I think it's better to have the if and else at
the same level. ie:

if (condition) {
doThis();
doThat();
}
else {
doWhatever();
andSoOn();
}

Anyway, it's a matter of style :)
which only takes 7 lines and is not much less readable. But I agree with
you!
I also agree. I love the fact that you can't produce bad indented code
in python

Regards,
Josef
 
C

Christopher T King

I don't like that style. I think it's better to have the if and else at
the same level. ie:

if (condition) {
doThis();
doThat();
}
else {
doWhatever();
andSoOn();
}

Pfft. We all know real men drop the braces altogether and just do it like
this:

if (condition)
doThis(),
doThat();
else
doWhatever(),
andSoOn();

;)
 
M

Mark 'Kamikaze' Hughes

Christopher T King said:
I dunno... that looks like some kind of voodoo chant to me... ;)

It can be written out in full:
set shiftwidth=4 smarttab expandtab autoindent backspace=indent,eol,start

I'm just an old-timey Vim geek, so I use the short version.
 
P

Peter Hansen

Josef said:
I also agree. I love the fact that you can't produce bad indented code
in python

Well, if you try hard enough, you can still get pretty brutal:

a = 5

if a == 6: print '6'
else:
if a == 7:
print '7'
\
elif a \
== 8:
print '8'
else:
print 'none of the above'


;-)

-Peter
 
P

Peter Hansen

Paramjit said:
Andrew Koenig proposed this in the following email:

http://groups.google.com/[email protected]

If link doesn't work - it was a post dated 2003-05-05 with the subject
"Re: PEP 315: Enhanced While Loop". There was a fairly long thread about
it back then.

Andrew's interpretation doesn't fit mine. I look at
the above and wonder why the code2 block doesn't
continue to execute until condition2 fails, and then
the code3 block loops repeatedly until condition3
fails...

-Peter
 
M

Mark 'Kamikaze' Hughes

Paramjit Oberoi said:
Andrew Koenig proposed this in the following email:
http://groups.google.com/[email protected]
If link doesn't work - it was a post dated 2003-05-05 with the subject
"Re: PEP 315: Enhanced While Loop". There was a fairly long thread about
it back then.

-1.

I do occasionally write loops where I do something, test and maybe
break, and then go on to do something else. However, the current while
construct does that just fine, and clearly expresses the start and end
of the code block:

linenum=1
while True:
line = raw_input()
if not line: break # or put on its own line, depending on taste
print "%05d:%s" % (linenum, line,)
linenum += 1

This confuses me as to where the loop starts and ends:
linenum=1
while True:
line = raw_input()
and while line:
print "%05d:%s" % (linenum, line,)
linenum += 1

I rarely use post-loop condition testing even in Java where it exists
and you can use assignment as an expression, let alone in Python. When
I do, an explicit if...break at the end works fine. Any loop that's
more than a few lines long, I usually extract into its own method.
 
N

Nikola Plejic

Peter said:
Well, if you try hard enough, you can still get pretty brutal:

a = 5

if a == 6: print '6'
else:
if a == 7:
print '7'
\
elif a \
== 8:
print '8'
else:
print 'none of the above'

Living proof that you actually can write really bad-looking code in
Python. Brutal is just the correct word for code like this :).

Nice one ;).
 
D

David Fraser

Peter said:
Andrew's interpretation doesn't fit mine. I look at
the above and wonder why the code2 block doesn't
continue to execute until condition2 fails, and then
the code3 block loops repeatedly until condition3
fails...

Which is exactly why the syntax proposal is bad - there are too many
interpretations of what it might mean...

David
 
J

Josef Meile

Well, if you try hard enough, you can still get pretty brutal:
a = 5

if a == 6: print '6'
else:
if a == 7:
print '7'
\
elif a \
== 8:
print '8'
else:
print 'none of the above'


;-)

-Peter
Oh yes, I forgot that the backslash "\" could break the indentation. But
I think nobody would do this intentionally. The word "brutal" says it.

Anyway, a good way of offuscating code; although is not what I do :)
 
A

Antoon Pardon

Op 2004-07-09 said:
-1.

I do occasionally write loops where I do something, test and maybe
break, and then go on to do something else. However, the current while
construct does that just fine, and clearly expresses the start and end
of the code block:

linenum=1
while True:
line = raw_input()
if not line: break # or put on its own line, depending on taste
print "%05d:%s" % (linenum, line,)
linenum += 1

This confuses me as to where the loop starts and ends:

This confusion may be nothing more than your unfamiliary which
such a construct.
linenum=1
while True:
line = raw_input()
and while line:
print "%05d:%s" % (linenum, line,)
linenum += 1

I rarely use post-loop condition testing even in Java where it exists
and you can use assignment as an expression, let alone in Python. When
I do, an explicit if...break at the end works fine.

That is not an argument. Before instructions like while, repeat etc
existed, we could use the equivallent of an if combined with a goto
and that worked fine too.
 
J

Jacek Generowicz

Chris Share said:
FYI, it's pretty simple...

I use vim for writing python in, with the following options:
:set et
:set tabstop=4

And how do I get it to send the function definition surrounding the
cursor to the Python interpreter with which it is currently
interacting ?

(Only half joking ... if there is a way to make vi(m) provide the same
interactive capabilities that Emacs does, then I would be able to
recommend it to those who insist on using vi in my courses and end up
trailing the rest of the class and slowing us all down.)
 
S

Steve Lamb

And how do I get it to send the function definition surrounding the
cursor to the Python interpreter with which it is currently
interacting ?

I dunno but it is possible considering vim can be compiled with Python
embedded. :p
 
M

Mark 'Kamikaze' Hughes

Antoon Pardon said:
This confusion may be nothing more than your unfamiliary which
such a construct.

No, I am quite certain that my confusion is due to the block
indentation aimlessly wandering out, then in, then out again, then in at
the end. And I'm supposed to visually identify that as a single flow of
execution? Bugger that.

I've programmed in dozens of languages. None but BASIC and various
assembly languages have a loop structure quite so ugly as that.
That is not an argument.

Actually, it is. You just didn't follow it. Python does not have
assignment as expressions, which is a feature that makes post-loop
testing more convenient. Python does have a practical solution to that
kind of loop already.

Python wins now because it is simple, and clear, and there's one
explicitly correct way to solve most tasks.
Before instructions like while, repeat etc
existed, we could use the equivallent of an if combined with a goto
and that worked fine too.

Ah, so your plan is to return to those golden times with equally
incoherent block structures. Excellent plan. Here was me thinking you
had no plan, but you did, and it's a *fine* plan. You go on ahead, I'll
be sure to follow your cutting-edge lead any day now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top