newbie: stani's python editor if-else

M

madzientist

hi,

two quick questions:

a) i am using SPE (latest version) and for some reason, when i type,
say

if 1==2:
print "not equal"
else:
print "equal"

the else is at the same indentation level as the preceding print
statement, and i get a syntax error

why doesn't spe automatically put the else at the level of the if
statement ? what am i dong wrong ? once i manually change the
indentation, the code snippet runs perfectly.

b) if this is not the group for such elementary questions, please do
let me know.

thanks !!!!

suresh
 
B

Bruno Desthuilliers

madzientist a écrit :
two quick questions:

a) i am using SPE (latest version) and for some reason, when i type,
say

if 1==2:
print "not equal"
else:
print "equal"

the else is at the same indentation level as the preceding print
statement, and i get a syntax error

Obviously. As you know, in Python, indentation is signifiant.
why doesn't spe automatically put the else at the level of the if
statement ?

I don't know. Perhaps you should ask the author ?
what am i dong wrong ?

Mmm, let's see.... Posting on the wrong place, perhaps ?-)
once i manually change the
indentation, the code snippet runs perfectly.
Indeed.

b) if this is not the group for such elementary questions, please do
let me know.

Ok, since you ask for it : this is not the group for problems related to
SPE.

You probably want the SPE mailing-list (if it exists), or whatever the
project page offers as a mean of sending feedback to the author.

Python-related questions, even elementary ones (provided you at least
took time to read the official tutorial), are of course welcome !-)

HTH
 
S

Steve Holden

madzientist said:
hi,

two quick questions:

a) i am using SPE (latest version) and for some reason, when i type,
say

if 1==2:
print "not equal"
else:
print "equal"

the else is at the same indentation level as the preceding print
statement, and i get a syntax error

why doesn't spe automatically put the else at the level of the if
statement ? what am i dong wrong ? once i manually change the
indentation, the code snippet runs perfectly.

b) if this is not the group for such elementary questions, please do
let me know.

thanks !!!!

suresh

The point here is that SPE isn't analyzing your source in sufficient
detail to recognize the need to "outdent" the else statement. Clearly
there could be any number of statements at the same indent level as the
first print statement.

For the record this is not a behavior restricted to SPE, but also occurs
in Wing IDE, PythonWin and Scite, for starters.

Technically you are correct in the example you give: an IDE could decide
to remove one level of indentation, but of course there's always the
"dangling else" problem: if one "if" statement is nested inside another
one, which indentation level would be appropriate for an "else"?

Generally speaking, only the programmer can know, and so generally you
are left to enforce these issues for yourself.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
K

kyosohma

hi,

two quick questions:

a) i am using SPE (latest version) and for some reason, when i type,
say

if 1==2:
print "not equal"
else:
print "equal"

the else is at the same indentation level as the preceding print
statement, and i get a syntax error

why doesn't spe automatically put the else at the level of the if
statement ? what am i dong wrong ? once i manually change the
indentation, the code snippet runs perfectly.

b) if this is not the group for such elementary questions, please do
let me know.

thanks !!!!

suresh

I agree with Steve. I have yet to see an IDE for Python (or anything
else) that unindents statements. Even IDLE, the Official IDE for
Python, doesn't do that.

Mike
 
G

Gregor Horvath

I agree with Steve. I have yet to see an IDE for Python (or anything
else) that unindents statements. Even IDLE, the Official IDE for
Python, doesn't do that.


emacs in python-mode

Just hit TAB and it unindents "else" in the given example.
Hit TAB again and it cylces in the possible indentations.

Gregor
 
S

Steve Holden

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of
(e-mail address removed)
Sent: Tuesday, September 11, 2007 8:26 AM
To: (e-mail address removed)
Subject: Re: newbie: stani's python editor if-else

hi,

two quick questions:

a) i am using SPE (latest version) and for some reason, when i type,
say

if 1==2:
print "not equal"
else:
print "equal"

the else is at the same indentation level as the preceding print
statement, and i get a syntax error

why doesn't spe automatically put the else at the level of the if
statement ? what am i dong wrong ? once i manually change the
indentation, the code snippet runs perfectly.

b) if this is not the group for such elementary questions, please do
let me know.

thanks !!!!

suresh
I agree with Steve. I have yet to see an IDE for Python (or anything
else) that unindents statements. Even IDLE, the Official IDE for
Python, doesn't do that.

IDLE (At least, IDLE 1.0.5) unindents in obvious situations. I think
it's only on break, continue, pass, and return statements, but there may
be others.
Wing and Scite also do that, but Emacs seems to have the nicest behavior.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
B

Bruno Desthuilliers

Steve Holden a écrit :
(snip)

The point here is that SPE isn't analyzing your source in sufficient
detail to recognize the need to "outdent" the else statement. Clearly
there could be any number of statements at the same indent level as the
first print statement.

For the record this is not a behavior restricted to SPE, but also occurs
in Wing IDE, PythonWin and Scite, for starters.

Technically you are correct in the example you give: an IDE could decide
to remove one level of indentation, but of course there's always the
"dangling else" problem: if one "if" statement is nested inside another
one, which indentation level would be appropriate for an "else"?

Generally speaking, only the programmer can know, and so generally you
are left to enforce these issues for yourself.

Emacs does a good job here - FWIW, it solves the "dangling else" problem
by deindenting one level, then it's up to you to deindent more if
appropriate. In practice, it turns out to be right most of the times,
and not a problem in the few special cases.
 
M

madzientist

Thanks, everybody, for the very very useful and kind responses.

There is a second reason why I asked the question about automatic de-
indenting. I am teaching myself Python partly so I can then help my
technically astute, but blind friend learn programming. For the many
reasons that Pythonistas like to cite often, I thought Python would be
a good choice to learn programming, and that perhaps the indentation
problem would be solved by the use of an intelligent editor.

But now I am not so sure, though I will try Emacs. Is there anyone
here with experience in such issues ? Maybe for her sake, I should
switch to learning Perl ;) ;)

More seriously, the added issue is that SPE uses spaces, not a single
tab to indent the lines, and I believe it is extremely tedious to use
screen-readers to keep track of blank spaces at the beginning of each
line. I have not tried it myself yet, but I will soon.

Is Python a bad choice for the blind programmer, as a result of a
tight linkage between visual aspects of the code and its function ? I
wish the site blindprogramming.com weren't so lifeless...

Thanks, Suresh
 
G

Gabriel Genellina

En Tue, 11 Sep 2007 14:28:35 -0300, Hamilton, William
Can you set SPE to use a single space rather than the typical four
spaces? Python should accept it just fine. You'll still have problems
reading other people's code. Maybe you can write a quick script that
converts code down to one-space indents.

The existing demo script reindent.py (in tools/Scripts) does exactly that,
but using 4 spaces. Just replace the number 4 in its source code by a
variable name (it occurs only twice) and it's done.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top