I use Notepad++ too (2013). Addigned a shortcut for menu / View / Show Symbol / Show whitespace and tab. In menu / Settings / Preferences / Tab Settings you can set whether to use tabs or (custom amount of, default 4) spaces. (I assigned a shortcut for changing that setting too but it's quite hacky and buggy.) If I receive a file with random indentation, it can uniformize them by pressing Ctrl+A Tab *Shift+Tab. It works more consistently than Eclipse for me.
–
naxaNov 28 '13 at 17:24

Yup, for the better. It makes it harder to write hard to read code.
–
EchoSep 19 '08 at 14:58

The question asked about available IDEs &c., and there are certainly such; thus, this is a bad answer.
–
Charles DuffySep 22 '08 at 23:30

2

@Xiong Chiamiov It isn't necessarily that the OP suck at dealing with the indention him/herself. It's perferctly possible for people to work on the same python file, and one uses this indentation, and the other uses a different one. Most IDEs won't render whitespaces by default, and it will drive you nuts if you're getting a whitespace error. The lack of tokens to indicate the scope makes it impossible to get an auto-indent that would at least show what the problem is.
–
CalythOct 8 '09 at 16:44

You should disable tab characters in your editor when you're working with Python (always, actually, IMHO, but especially when you're working with Python). Look for an option like "Use spaces for tabs": any decent editor should have one.

Yes, that is true. But Python has one interpretation of tabs and your text editor may have another. For this reason, the Python language manual recommends not mixing tabs and spaces in indentation. IMHO, the easiest way to guarantee this is to always use spaces.
–
Chris ConwaySep 1 '09 at 21:10

It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment.

Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.

I agree with justin and others -- pick a good editor and use spaces rather than tabs for indentation and the whitespace thing becomes a non-issue. I only recently started using Python, and while I thought the whitespace issue would be a real annoyance it turns out to not be the case. For the record I'm using emacs though I'm sure there are other editors out there that do an equally fine job.

If you're really dead-set against it, you can always pass your scripts through a pre-processor but that's a bad idea on many levels. If you're going to learn a language, embrace the features of that language rather than try to work around them. Otherwise, what's the point of learning a new language?

Not really. There are a few ways to modify whitespace rules for a given line of code, but you will still need indent levels to determine scope.

You can terminate statements with ; and then begin a new statement on the same line. (Which people often do when golfing.)

If you want to break up a single line into multiple lines you can finish a line with the \ character which means the current line effectively continues from the first non-whitespace character of the next line. This visually appears violate the usual whitespace rules but is legal.

My advice: don't use tabs if you are having tab/space confusion. Use spaces, and choose either 2 or 3 spaces as your indent level.

A good editor will make it so you don't have to worry about this. (python-mode for emacs, for example, you can just use the tab key and it will keep you honest).

But why would want to write code like in the first example? It's harder to read.
–
EchoSep 19 '08 at 15:00

You would only input it the first way. Perhaps you have physical limitations, such as being visually impaired and using a screen reader, or are copy/pasting code from something that eats tabs.
–
RyanSep 22 '08 at 3:32

How does it know that the second "if" goes inside the "elif", or that the final "else" doesn't?
–
detlyApr 22 '10 at 23:32

The same way that other compilers match up parenthesis. It keeps it on a stack :)
–
RyanApr 23 '10 at 2:44

Ah, I did not notice the end if lines! Makes more sense.
–
detlyApr 23 '10 at 11:39

Getting your indentation to work correctly is going to be important in any language you use.

Even though it won't affect the execution of the program in most other languages, incorrect indentation can be very confusing for anyone trying to read your program, so you need to invest the time in figuring out how to configure your editor to align things correctly.

Python is pretty liberal in how it lets you indent. You can pick between tabs and spaces (but you really should use spaces) and can pick how many spaces. The only thing it requires is that you are consistent which ultimately is important no matter what language you use.

I'm surprised no one has mentioned IDLE as a good default python editor. Nice syntax colors, handles indents, has intellisense, easy to adjust fonts, and it comes with the default download of python. Heck, I write mostly IronPython, but it's so nice & easy to edit in IDLE and run ipy from a command prompt.

Oh, and what is the big deal about whitespace? Most easy to read C or C# is well indented, too, python just enforces a really simple formatting rule.

Many Python IDEs and generally-capable text/source editors can handle the whitespace for you.

However, it is best to just "let go" and enjoy the whitespace rules of Python. With some practice, they won't get into your way at all, and you will find they have many merits, the most important of which are:

Because of the forced whitespace, Python code is simpler to understand. You will find that as you read code written by others, it is easier to grok than code in, say, Perl or PHP.

Whitespace saves you quite a few keystrokes of control characters like { and }, which litter code written in C-like languages. Less {s and }s means, among other things, less RSI and wrist pain. This is not a matter to take lightly.

In Python, indentation is a semantic element as well as providing visual grouping for readability.

Both space and tab can indicate indentation. This is unfortunate, because:

The interpretation(s) of a tab varies
among editors and IDEs and is often
configurable (and often configured).

OTOH, some editors are not
configurable but apply their own
rules for indentation.

Different sequences of
spaces and tabs may be visually
indistinguishable.

Cut and pastes can alter whitespace.

So, unless you know that a given piece of code will only be modified by yourself with a single tool and an unvarying config, you must avoid tabs for indentation (configure your IDE) and make sure that you are warned if they are introduced (search for tabs in leading whitespace).

And you can still expect to be bitten now and then, as long as arbitrary semantics are applied to control characters.

The real answer to your question is that if you are going to use the language you need to learn its syntax. Just as an error in indenting python can generate a compiler error, an error using braces in various other languages can also generate a compiler error.

Even worse it can be silently misinterpreted by the compiler to do the wrong thing. This is particularly dangerous when the indenting doesn't match the desired meaning. I.e. in many other languages:

Check the options of your editor or find an editor/IDE that allows you to convert TABs to spaces. I usually set the options of my editor to substitute the TAB character with 4 spaces, and I never run into any problems.

Yes, there is a way. I hate these "no way" answers, there is no way until you discover one.

And in that case, whatever it is worth, there is one.

I read once about a guy who designed a way to code so that a simple script could re-indent the code properly. I didn't managed to find any links today, though, but I swear I read it.

The main tricks are to always use return at the end of a function, always use pass at the end of an if or at the end of a class definition, and always use continue at the end of a while. Of course, any other no-effect instruction would fit the purpose.

Then, a simple awk script can take your code and detect the end of block by reading pass/continue/return instructions, and the start of code with if/def/while/... instructions.

Of course, because you'll develop your indenting script, you'll see that you don't have to use continue after a return inside the if, because the return will trigger the indent-back mechanism. The same applies for other situations. Just get use to it.

If you are diligent, you'll be able to cut/paste and add/remove if and correct the indentations automagically. And incidentally, pasting code from the web will require you to understand a bit of it so that you can adapt it to that "non-classical" setting.

The "whitespace thing" in Python exists specifically to avoid requiring any specific closing mark or word at the end of if/while/def blocks. How would you distinguish the end of an if block within a class block, from the end of the class block itself? There is only one standard no-op statement, pass, which is valid in both contexts.
–
Dan LenskiJun 19 '14 at 17:26