Question

At the moment I have never had a problem with whitespace in Python (although I've only used it in two projects and I was the only programmer). What are some potential pitfalls with whitespace and indentation in Python for someone learning the language?

Was it helpful?

Solution

yeah there are some pitfalls, but most of the time, in practice, they turn out to be enemy windmills of the Quixotic style, i.e. imaginary, and nothing to worry about in reality.

I would estimate that the pitfalls one is most likely to encounter are (including mitigating steps identified):

  1. working with others a.k.a. collaboration

    a. if you have others which for whatever reason refuse to adhere to PEP 8, then it could become a pain to maintain code. I've never seen this in practice once I point out to them the almost universal convention for python is indent level == four spaces

    b. get anyone/everyone you work with to accept the convention and have them figure out how to have their editor automatically do it (or better yet, if you use the same editor, show them how to configure it) such that copy-and-paste and stuff just works.

  2. having to invest in a "decent" editor other than your current preferred one, if your current preferred editor is not python friendly -- not really a pitfall, more an investment requirement to avoid the other pitfalls mentioned associated with copy-and-paste, re-factoring, etc. stop using Notepad and you'll thank yourself in the morning.

    a. your efficiency in editing the code will be much higher under an editor which understands python

    b. most modern code editors handle python decently. I myself prefer GNU Emacs, and recent versions come with excellent python-mode support out-of-the-box. The are plenty of other editors to explore, including many free alternatives and IDEs.

    c. python itself comes out of the box with a "smart" python editor, idle. Check it out if you are not familiar, as it is probably already available with your python install, and may even support python better than your current editor. PyCrust is another option for a python editor implemented in python, and comes as part of wxPython.

  3. some code generation or templating environments that incorporate python (think HTML generation or python CGI/WSGI apps) can have quirks

    a. most of them, if they touch python, have taken steps to minimize the nature of python as an issue, but it still pops up once in a while.

    b. if you encounter this, familiarize yourself with the steps that the framework authors have already taken to minimize the impact, and read their suggestions (and yes they will have some if it has ever been encountered in their project), and it will be simple to avoid the pitfalls related to python on this.

OTHER TIPS

It can be confusing in some editors where one line is indented with spaces and the next is indented with a tab. This is confusing as the indentation looks the same but causes an error.

Also when your copying code, if your editor doesn't have a function to indent entire blocks, it could be annoying fixing all the indentation.

But with a good editor and a bit of practice, this shouldn't be a problem. I personally really like the way Python uses white space.

That actually kept me away from Python for a while. Coming from a strong C background, I felt like I was driving without a seat belt.

It was aggravating when I was trying to fill up a snippet library in my editor with boilerplate, frequently used classes. I learn best by example, so I was grabbing as many interesting snippets as I could with the aim of writing a useful program while learning.

After I got in the habit of re-formatting everything that I borrowed, it wasn't so bad. But it still felt really awkward. I had to get used to a dynamically typed language PLUS indentation controlling my code.

It was quite a leap for me :)

When I look at C and Java code, it's always nicely indented.

Always. Nicely. Indented.

Clearly, C and Java folks spend a lot of time getting their whitespace right.

So do Python programmers.

Whitespace block delimiters force a certain amount of code formatting, which seems to irritate some programmers. Some in our shop seem to be of the attitude that they are too busy, or can't be bothered to pay attention to formatting standards, and a language that forces it rubs them raw. Sometimes the same folks gripe when others do not follow the same patterns of putting curly braces on a new line ;)

I find that Python code from the web is more commonly "readable", since this minor formatting requirement is in place. IMO, this requirement is a very useful feature.

IIRC, does not Haskell, OCaml (#light), and F# also use whitespace in the same fashion? For some reason, I have not seen any complaints about these languages.

Long ago, in and environment far, far away, there were languages (such as RPG) that depended on the column structure of punch cards. This was a tedious and annoying system, and led to many errors, and newer languages such as BASIC, pascal, and so forth were designed without this dependency.

A generation of programmers were trained on these languages and told repeatedly that the freedom to put anything anywhere was a wonderful feature of the newer languages, and they should be grateful. The freedom was used, abused, and calibrated (cf the IOCC) for many years.

Now the pendulum has begun to swing back, but many people still remember that forced layout is bad in some way vague, and resist it.

IMHO, the thing to do is to work with languages on their own terms, and not get hung up on tastes-great-less-filling battles.

When python programmers don't follow the common convention of "Use 4 spaces per indentation level" defined in PEP 8. (If your a python programmer and haven't read it please do so)

Then you run into copy paste issues.

Some people say that they don't like python indentation, because it can cause errors, which would be immensely hard to detect in case if tabs and spaces are mixed. For example:

1 if needFrobnicating:
2    frobnicate()
3 update()

Depending on the tab width, line 3 may appear to be in the same block as line 2, or in the enclosing block. This won't cause runtime or compile error, but the program would do unexpected thing.

Though I program in python for 10 years and never seen an error caused by mixing tabs and spaces

Pick a good editor. You'd want features such as:

  1. Automatic indentation that mimics the last indented line
  2. Automatic indentation that you can control (tabs vs. spaces)
  3. Show whitespace characters
  4. Detection and mimicking of whitespace convention when loading a file

For example, Vim lets me highlight tabs with these settings:

set list
set listchars=tab:\|_
highlight SpecialKey ctermbg=Red guibg=Red
highlight SpecialKey ctermfg=White guifg=White

Which can be turned off at any time using:

set nolist

IMO, I dislike editor settings that convert tabs to spaces or vice versa, because you end up with a mix of tabs and spaces, which can be nasty.

I used to think that the white space issues was just a question of getting used to it.

Someone pointed out some serious flaws with Python indentation and I think they are quite valid and some subconcious understanding of these is what makes experienced programs nervious about the whole thing:-

  • Cut and paste just doesnt work anymore! You cannot cut boiler plate code from one app and drop it into another app.
  • Your editor becomes powerless to help you. With C/Jave etc. there are two things going on the "official" curly brackets indentation, and, the "unnofficial" white space indentation. Most editors are able reformat hte white space indentation to match the curly brackets nesting -- which gives you a string visual clue that something is wrong if the indentation is not what you expected. With pythons "space is syntax" paradigm your editor cannot help you.
  • The sheer pain of introducing another condition into already complex logic. Adding another if then else into an existing condition involves lots of silly error prone inserting of spaces on many lines line by hand.
  • Refactoring is a nightmare. Moving blocks of code around your classes is so painful its easier to put up with a "wrong" class structure than refactor it into a better one.

If you use Eclipse as your IDE, you should take a look at PyDev; it handles indentation and spacing automatically. You can copy-paste from mixed-spacing sources, and it will convert them for you. Since I started learning the language, I've never once had to think about spacing.

And it's really a non-issue; sane programmers indent anyway. With Python, you just do what you've always done, minus having to type and match braces.

Pitfalls

  • It can be annoying posting code snippets on web sites that ignore your indentation.

  • Its hard to see how multi-line anonymous functions (lambdas) can fit in with the syntax of the language.

  • It makes it hard to embed Python in HTML files to make templates in the way that PHP or C# can be embedded in PHP or ASP.NET pages. But that's not necessarily the best way to design templates anyway.

  • If your editor does not have sensible commands for block indent and outdent it will be tedious to realign code.

Advantages

  • Forces even lazy programmers to produce legible code. I've seen examples of brace-language code that I had to spend hours reformatting to be able to read it...

  • Python programmers do not need to spend hours discussing whether braces should go at the ends of lines K&R style or on lines on their own in the Microsoft style.

  • Frees the brace characters for use for dictionary and set expressions.

  • Is automatically pretty legible

The problem is that in Python, if you use spaces to indent basic blocks in one area of a file, and tabs to indent in another, you get a run-time error. This is quite different from semicolons in C.

This isn't really a programming question, though, is it?

The only trouble I've ever had is minor annoyances when I'm using code I wrote before I settled on whether I liked tabs or spaces, or cutting and posting code from a website.

I think most decent editors these days have a convert tabs-to-spaces and back option. Textmate certainly does.

Beyond that, the indentation has never caused me any trouble.

If your using emacs, set a hard tab length of 8 and a soft tab length of 4. This way you will be alterted to any extraneous tab characters. You should always uses 4 spaces instead of tabs.

One drawback I experienced as a beginner whith python was forgetting to set softtabs in my editors gave me lots of trouble.

But after a year of serious use of the language I'm not able to write poorly indented code anymore in any other language.

No, I would say that is one thing to which I can find no downfalls. Yes, it is no doubt irritating to some, but that is just because they have a different habit about their style of formatting. Learn it early, and it's gonna stick. Just look how many discussions we have over a style matter in languages like C, Cpp, Java and such. You don't see those (useless, no doubt) discussions about languages like Python, F77, and some others mentioned here which have a "fixed" formatting style.

The only thing which is variable is spaces vs. tabs (can be changed with a little trouble with any good editor), and amount of spaces tab accounts for (can be changed with no trouble with any good editor). Voila ! Style discussion complete.

Now I can do something useful :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top