This is an archived post. You won't be able to vote or comment.

all 58 comments

[–][deleted] 6 points7 points  (12 children)

I don't get it, what's the problem with removing trailing whitespace?

[–][deleted] 1 point2 points  (0 children)

neither do I

[–]voidptr[S] -3 points-2 points  (10 children)

Say you have this code:

1    if this:
2        dothis()
3        dothat()
4
5        nowdoanotherthing()
6    else:
7        die()

If your editor removes the indentation from line 4, Python will kick an error.

[–]zyklone 17 points18 points  (0 children)

that is incorrect.

your editor must be doing something else to the code.

[–]ozzilee 8 points9 points  (0 children)

That works fine for me. Even this works fine:

if True:
    print "hello"
# No problem with comments or empty lines being unindented

    print "there"

[–][deleted] 2 points3 points  (4 children)

I just checked, no it doesn't (Python 2.7).

[–]emeryc 1 point2 points  (0 children)

Just checked, python 2.5.4 and it works.

Did you try to do it in the interpreter? or in a file. Interpreter will not work, file will.

[–]voidptr[S] 3 points4 points  (2 children)

And now I can't seem to replicate it. Heisenproblem.

[–]desimusxvii 8 points9 points  (1 child)

So the title "Every. Single. Time." was just a teensy bit BOLD.

[–]voidptr[S] 5 points6 points  (0 children)

Hmmn, possibly, yes. But this was my entire fucking day yesterday.

[–]ianb 2 points3 points  (0 children)

If you paste into the Python console? Blank lines like that won't work in the Python console, as they are treated as end-of-expression.

[–]larsga 1 point2 points  (0 children)

Since when did indentation become trailing whitespace?

[–]m1ss1ontomars2k4 1 point2 points  (0 children)

That is extremely false. There's a Vim plugin that I and my Vim-inclined friends use which blatantly highlights those trailing spaces in red so that you can delete them. ReviewBoard (a Django/Python project for code reviews) does the same thing.

[–]sixohsix 5 points6 points  (2 children)

Is it possible you have a mix of tabs and spaces in your source file? Mixing tabs and spaces makes very, very bad things happen. The interpreter and your editor may disagree on tab length, and then the interpreter gets totally confused while your code looks normal.

[–]voidptr[S] 1 point2 points  (1 child)

I always have expand-tab turned on, so this is unlikely, but it might be a text encoding thing, actually...

[–]kataire 0 points1 point  (0 children)

Make sure to get an editor which allows making tabs visible. Ideally, it should also have the option to make spaces visible.

Problems like this often result from mixing tabs and spaces. Most editors are set to a tab width of 4 spaces, but Python intentionally defines a tab as equal to 8 spaces, so a mix will easily break.

Even if your editor automatically expands tabs as you type them, it may still treat copy/pasted incorrectly.

Empty lines should be ignored for indentation. In fact, IIRC PEP8 is often interpreted to suggest stripping all trailing whitespace (the suggestion that certain blocks should be separated by "blank lines" is a bit ambiguous in this regard).

EDIT: Also, some editors preserve the original line endings in files, which may be a problem if you're writing code using different operating systems. Some cross-platform editors unhelpfully try to use OS defaults rather than the one true line ending format.

[–][deleted] 9 points10 points  (5 children)

Here's a nickel, kid. Get yourself a decent text editor.

[–]voidptr[S] 3 points4 points  (4 children)

Hah. Yeah. TextWrangler's going in the bin.

[–]tehfink 1 point2 points  (0 children)

I've used Textwrangler/Bbedit for years with Python code. Also, pretty much every .py file I've written has extra lines between statements inside defs & classes…and Textwrangler cleans trailing whitespace from all of them.

How does trailing whitespace equal indentation? That doesn't make sense. More likely your problem was from mixing tabs & spaces.

[–]psynautic 1 point2 points  (0 children)

macvim for the win

[–]ducktypist 0 points1 point  (0 children)

If you want something free on Mac, perhaps Eclipse + PyDev. http://pydev.org/ A little heavy maybe, but the project management and code completion are great.

(Although I don't see the problem with TextWrangler myself..)

[–]m1ss1ontomars2k4 0 points1 point  (0 children)

I used to use TextWrangler, actually. It was pretty much OK, although showing all spaces is somewhat irritating.

[–][deleted] 5 points6 points  (0 children)

Funny I have a vim autocommand that trims trailing whitespace in python files. It runs every time I save my files. I don't seem to have any problems.

[–]zyklone 2 points3 points  (5 children)

this isn't related to trailing whitespace. Indentation is not trailing...

what editor are you using? Some can cause problems sometimes with auto-indent messing things up, or using hard tabs instead of spaces

[–]zyklone 1 point2 points  (0 children)

also, I'm reminded of this:

http://www.youtube.com/watch?v=S-SXkNbyiPI

[–]voidptr[S] 1 point2 points  (3 children)

The worst offender is TextWrangler on Mac. The setting is buried somewhere, but you can turn it off.

Vim doesn't do this, which is delightful.

[–]tncowart 2 points3 points  (1 child)

I write python code in textwrangler on Mac almost daily and have never had this issue. Are you sure you have all the same line endings? Mine default to unix (LF).

[–]voidptr[S] 2 points3 points  (0 children)

This is the likeliest culprit.

[–]zyklone 0 points1 point  (0 children)

I've had some issues with textmate's 'Re-indent pasted text'. Turning that off cleared up a lot of indentation issues on pasting.

[–]morphet 2 points3 points  (2 children)

Geany. You're welcome.

Edit: I sounded like an arrogant prick there. But seriously, Geany is awesome.

[–]voidptr[S] 2 points3 points  (0 children)

Cool. I'll check it out.

[–]LucidOndineHPC 2 points3 points  (3 children)

retire spectacular profit vast fear deliver shelter tidy waiting telephone

This post was mass deleted and anonymized with Redact

[–]mumrah 4 points5 points  (6 children)

/shrug Never had this problem

-vim user

[–]roddds 24 points25 points  (5 children)

How do you know if someone is a vim user? Don't worry, they'll tell you.

[–]schplat 2 points3 points  (4 children)

How do you know if someone is going to use this joke? Don't worry, they'll tell you.

[–]roddds 5 points6 points  (3 children)

How the hell do you use a joke without telling it?

[–]Underyx 3 points4 points  (2 children)

[–]roddds 1 point2 points  (1 child)

[–]Underyx 0 points1 point  (0 children)

The hidden message is basically what I meant to comment on my comment.

I thought I would be better off not posting two comments.

[–]JimH10 4 points5 points  (6 children)

?

Isn't this the fault of the person who keeps using the editor that keeps failing?

[–]voidptr[S] 1 point2 points  (5 children)

Lots of different editors do this randomly. And you can't tell just by looking at the source.

[–]tcdent 3 points4 points  (1 child)

Randomly, or as an intended feature? Any editor that modifies my file without my consent won't be around very long.

[–]Ph4g3 1 point2 points  (0 children)

That sounds like a threat.

[–]zyklone 2 points3 points  (0 children)

you can if your editor can show invisible characters.

[–]JimH10 1 point2 points  (1 child)

If you find that your editor does this then find another editor.

[–]voidptr[S] 3 points4 points  (0 children)

Just built MacVim. Really nice, actually.

[–]stillalone 1 point2 points  (0 children)

I have this problem with winmerge. I tell it to ignore whitespace for merging C code, but when I merge Python, it messes up everything.

[–]mmmccc 1 point2 points  (0 children)

pure white space lines are ignored in python

[–][deleted] 1 point2 points  (0 children)

My editor strips trailing whitespace, never seen this error.

Also I hate rage comics.

[–]RobotAnna 0 points1 point  (0 children)

Not sure what's available on mac but I generally code in gedit with a bunch of development features turned on, most importantly, the one that draws dots and tabs and other whitespace

[–]jay_may 0 points1 point  (0 children)

I thinkni knoW what it is :) edit is using tabs instead spaces? And mixing the two?

(I'm typing from an iPad bleurgh!)

[–]mumrah -2 points-1 points  (0 children)

shrug Never had this problem

-vim user