Python Indentation Problems

D

diffuser78

I am a newbie to Python. I am mainly using Eric as the IDE for coding.
Also, using VIM and gedit sometimes.

I had this wierd problem of indentation. My code was 100% right but it
wont run because indentation was not right. I checked time and again
but still no success. I rewrote the code over again in VI and it ran.

Can you please explain whats the trick behind the correct indentation.

Thanks
 
G

gene tani

I am a newbie to Python. I am mainly using Eric as the IDE for coding.
Also, using VIM and gedit sometimes.

I had this wierd problem of indentation. My code was 100% right but it
wont run because indentation was not right. I checked time and again
but still no success. I rewrote the code over again in VI and it ran.

Can you please explain whats the trick behind the correct indentation.

These aren't really tricks or secrets:

http://www.python.org/doc/essays/styleguide.html
http://www.logilab.org/projects/pylint/documentation

Different editors, e.g. komodo and textmate, let you look at tabs and
spaces graphically
And avoid pasting other peoples' code into your modules. Keep them in
separate modules
 
D

Dennis Lee Bieber

Can you please explain whats the trick behind the correct indentation.
Don't mix tabs and spaces; either use ALL spaces for indentation, or
all tabs.

Most of the editors with a decent Python mode have been rigged up to
translate the tab key into "n" spaces. If you have a real tab character
in the file, the parser considers it equal to 8 spaces -- regardless of
what it shows on screen.
--
 
R

Renato

If you use vi (vim, I hope), then place something like this in your
..vimrc

set ts=4
set sw=4
set expandtab
set ai

There are a lot more tricks for python in vim (and plugins, and
helpers, and so on), but this is the starting point: tabstops of 4
places, autoconverted in spaces. Also, when shifting text with < or >
it moves 4 spaces.
 
S

sudharsh

As far as i know, gedit is the weak link, this is because of the way it
handles its whitespaces, had that trouble myself though not this
*severe*
 
B

bruno at modulix

I am a newbie to Python. I am mainly using Eric as the IDE for coding.
Also, using VIM and gedit sometimes.

I had this wierd problem of indentation. My code was 100% right but it
wont run because indentation was not right.

If indentation is not right, then your code is not 100% right !-)
I checked time and again
but still no success. I rewrote the code over again in VI and it ran.

Can you please explain whats the trick behind the correct indentation.

1/ use spaces not tabs. Hint : all decent code editors I know have a way
to configure this - just make sure all the editors you use have the
correct settings.
2/ preferably, stick to 4 spaces indent
3/ if you run into troubles, check the code with tabnanny:
http://effbot.org/librarybook/tabnanny.htm
 
T

Terry Hancock

On 26 Feb 2006 22:21:26 -0800
I am a newbie to Python. I am mainly using Eric as the IDE
for coding. Also, using VIM and gedit sometimes.

I had this wierd problem of indentation. My code was 100%
right but it wont run because indentation was not right. I
checked time and again but still no success. I rewrote the
code over again in VI and it ran.

Can you please explain whats the trick behind the correct
indentation.

I'm not sure about Eric, but I found that when using the
Python interpreter through a terminal window, it's often
confusingly fastidious about tabs versus spaces. You would
think that in an interactive context the interpreter would
be smart enough to fix these errors on input (e.g. by
implementing a "convert tabs to spaces" policy by default
-- I've already configured my gvim editor to do that, and
while it's not obviously the right thing to do in an
editor it seems like a no-brainer for an editing mode
whose only purpose is to run Python), but it apparently
doesn't (or didn't anyway -- either I've gotten better
about not mixing them, or it may have been fixed, or else
I'm just lucky to finally have a terminal that agrees with
the interpreter on the width of tabs).

In any case, it's good practice not to mix tabs and spaces.

I actually recommend using just tabs when playing with
the interpreter (it's faster) -- but use spaces in your real
source code files.
 
J

John M. Gabriele

Renato said:
If you use vi (vim, I hope), then place something like this in your
.vimrc

set ts=4
set sw=4
set expandtab
set ai

Or, more verbose:

set tabstop=4
set shiftwidth=4

set autoindent
There are a lot more tricks for python in vim (and plugins, and
helpers, and so on), but this is the starting point: tabstops of 4
places, autoconverted in spaces. Also, when shifting text with < or >
it moves 4 spaces.

Also possibly useful:

set shiftround

---John
 
P

Petr Jakes

Eric3 works great with spaces, tabs and even when imported code
indentation is "mixed". I have got problems trying to import "mixed)
code from other people.

Settings > Preferences > Editor > General
Tab width 8 Indentation width 4 (reasons why 8 and 4 are mentioned in
previous postings in this thread)

Than you can set the checkboxes:
- Use tabs for indentation
- Convert tabs upon open
- Tab key indents
- Auto indentation
- Show Indentation Guides

You can also see the different marks for tabs and for spaces in Eric3
(if set), so you will see, where is the problem in you code.

My favorite options (I am trying to use spaces for indentation
strictly) are:
=====================================
- "Use tabs for indentation" - unchecked
- "Convert tabs upon open" - checked
=====================================

Because of that I am not getting to the troubles with indentation
anymore.

HTH
Petr Jakes
 

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,057
Latest member
KetoBeezACVGummies

Latest Threads

Top