By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

Python Indentation Problems

P: n/a
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

Feb 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

di********@gmail.com wrote:
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

Feb 27 '06 #2

P: n/a
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.

--
bye,
Renato

Feb 27 '06 #3

P: n/a
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*

Feb 27 '06 #4

P: n/a
di********@gmail.com wrote:
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

Thanks

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Feb 28 '06 #5

P: n/a
On 26 Feb 2006 22:21:26 -0800
di********@gmail.com wrote:
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.

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 28 '06 #6

P: n/a
Renato wrote:
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
--
(remove zeez if demunging email address)
Mar 1 '06 #7

P: n/a
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

Mar 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.