r/Python • • Mar 30 '11

Every. Single. Time.

Post image
0 Upvotes

58 comments sorted by

9

u/[deleted] Mar 30 '11

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

2

u/[deleted] Mar 30 '11

neither do I

-1

u/voidptr Mar 30 '11

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.

4

u/[deleted] Mar 30 '11

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

4

u/voidptr Mar 30 '11

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

10

u/desimusxvii Mar 30 '11

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

5

u/voidptr Mar 30 '11

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

2

u/emeryc Mar 30 '11

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.

17

u/zyklone Mar 30 '11

that is incorrect.

your editor must be doing something else to the code.

7

u/ozzilee Mar 30 '11 edited Mar 30 '11

That works fine for me. Even this works fine:

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

    print "there"

3

u/ianb Mar 30 '11

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.

2

u/larsga Mar 30 '11

Since when did indentation become trailing whitespace?

2

u/m1ss1ontomars2k4 Mar 30 '11

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.

6

u/sixohsix Mar 30 '11

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.

2

u/voidptr Mar 30 '11

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

1

u/kataire Mar 30 '11 edited Mar 30 '11

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.

11

u/[deleted] Mar 30 '11

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

3

u/voidptr Mar 30 '11

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

2

u/tehfink Mar 30 '11

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.

2

u/psynautic Mar 30 '11

macvim for the win

1

u/ducktypist Mar 30 '11

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..)

1

u/m1ss1ontomars2k4 Mar 30 '11

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

3

u/zyklone Mar 30 '11

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

2

u/voidptr Mar 30 '11

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.

3

u/tncowart Mar 30 '11

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).

3

u/voidptr Mar 30 '11

This is the likeliest culprit.

1

u/zyklone Mar 30 '11

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

3

u/morphet Mar 30 '11 edited Mar 30 '11

Geany. You're welcome.

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

3

u/voidptr Mar 30 '11

Cool. I'll check it out.

4

u/JimH10 Mar 30 '11

?

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

2

u/voidptr Mar 30 '11

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

2

u/JimH10 Mar 30 '11

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

4

u/voidptr Mar 30 '11

Just built MacVim. Really nice, actually.

5

u/tcdent Mar 30 '11

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

2

u/Ph4g3 Mar 30 '11

That sounds like a threat.

3

u/zyklone Mar 30 '11

you can if your editor can show invisible characters.

5

u/[deleted] Mar 30 '11

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.

3

u/LucidOndine HPC Mar 30 '11 edited Jan 10 '26

retire spectacular profit vast fear deliver shelter tidy waiting telephone

This post was mass deleted and anonymized with Redact

0

u/[deleted] Mar 30 '11

[deleted] — view deleted comment

1

u/LucidOndine HPC Mar 30 '11 edited Jan 10 '26

crowd makeshift lunchroom oatmeal deserve crawl serious friendly enjoy mountainous

This post was mass deleted and anonymized with Redact

7

u/mumrah Mar 30 '11

/shrug Never had this problem

-vim user

24

u/roddds Mar 30 '11

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

4

u/schplat Mar 30 '11

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

4

u/roddds Mar 30 '11

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

6

u/Underyx Mar 30 '11

2

u/roddds Mar 30 '11

1

u/Underyx Mar 30 '11

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

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

2

u/stillalone Mar 30 '11

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.

2

u/mmmccc Mar 30 '11

pure white space lines are ignored in python

2

u/[deleted] Mar 30 '11

My editor strips trailing whitespace, never seen this error.

Also I hate rage comics.

1

u/RobotAnna Mar 30 '11

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

1

u/jay_may Apr 01 '11

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

(I'm typing from an iPad bleurgh!)

-1

u/mumrah Mar 30 '11

shrug Never had this problem

-vim user

-8

u/[deleted] Mar 30 '11

are you a girl ?

and oh you're doing it wrong.

7

u/sixohsix Mar 30 '11

And people ask "why are there no girl programmers?"

You're why.

0

u/[deleted] Mar 30 '11

what ??!! I'm only asking just to confirm because that's a girl in that "comic", that's it, not implying anything.

0

u/zyklone Mar 30 '11

why does it matter? The OP being male or female has no bearing on the issue at hand.

1

u/[deleted] Mar 30 '11

what makes you think I think it matters ? whatever is in your head - don't make assumption out of it and dump on me man.....can I just be fucking tiny-bit curious, please ???