473,785 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5876

di********@gmai l.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********@gmai l.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********@gmai l.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*****@Anansi Spaceworks.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
6370
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 whitespace as part of the syntax. Definitely NOT. Without addressing the question of HOW Python came by the idea of using indentation to indicate block structure, it clearly couldn't have been from Fortran, because what Python does and what Fortran...
699
34240
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
105
5205
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 indenting is what makes Python unique) and sort of Ruby in its use of prefixes to define scoping etc (although there is no reference to this trait being borrowed from Ruby). It also quotes Self as being an influence.
145
6380
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 unecessary? Here is an example of a Python class with all three types of methods (instance, static, and class methods). # Example from Ch.23, p.381-2 of Learning Python, 2nd ed. class Multi:
147
7787
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 better freedom to the user? Is there any plausible reason behind this? Cheers! Sateesh
267
10849
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 http://www.artima.com/weblogs/viewpost.jsp?thread=147358
852
28732
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 my general education. Mark
19
1733
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 any issues with Python's ways of indentation, but that conversation got me thinking about the issue. Consider the following indentation rules: 1. Blocks begin with a line ending in ":" 2. A line on the same indentation level or lower ends a...
15
2396
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 work(ed) for never had time or money for courses, I am now in the awkward position that I am -still- developing classic ASP. In it, I have become quite resourceful, I wrote a framework using WSC (windows scripting components) to separate logic from...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.