473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Python Indentation Problems

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
7 5853

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael Chermside | last post by:
richardc writes: > Im no expert but wouldnt you accept that Python has 'borrowed' FORTRAN's > fixed format syntax. I can only think of Python and FORTRAN off the top of > my head which use...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
105
by: Peter Hickman | last post by:
Well after all this discussion it would appear that a 'Python like' language has appeared => Prothon. http://www.prothon.org/index.html Very alpha, sort of like Python (if you consider the...
145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
147
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
19
by: Sam | last post by:
A friend of mine is picking up some Python and is frustrated by Python's indentation rules (http://greatbiggary.livejournal.com/ 260460.html?thread=1835884#t1835884). Personally, I've never had...
15
by: erik.oosterwaal | last post by:
Hi All, I have been developing websites in classic asp using VB script for a long while now. Due to the fact that I also took a detour to developing ColdFusion, and the fact the companies I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.