Wandering Thoughts archives


I think I'm going to shift my style of Python indentation

Ever since I started programming what is now a very long time ago, I've indented almost every language I've worked in in the same simple way; each indentation level is 8 spaces wide and I use real tab characters. I'm not sure where this came from but it's clearly the lazy way to use non-autoindenting editors to write code, since all you do to indent each line is hit your tab key a few times. Need to dedent a line? Delete a leading tab or two. When I started using GNU Emacs for C code, I duplicated this basic approach in my Emacs autoindentation configuration; when I started writing Python, I copied my basic C indenting options over to Python (and of course it stayed easy to write some Python this way in vi or sam).

There are two issues with this. First, this is not the PEP 8 Python style, which is 4-space indent levels and indenting using spaces only. When my co-workers write Python from scratch it seems that they usually wind up using the PEP 8 style (possibly because it's the default Python style for modern smart auto-indenting editors), and of course there's a bunch of other code out there in the world that's in this style. My code at least stands out as different, which is not necessarily a good thing. Second, modern versions of Emacs have a little problem with my style. The little problem isn't fatal; I can usually edit code in my style without problems. But it's irritating and it makes working in my style a little bit more troublesome.

In the old days I would have grimly stuck to my guns here. I have a Python formatting style and it works for me and I'm very accustomed to it and so on, after all. These days, well, I think I'm now willing to see the writing on the wall here. Life will be simpler if I conform to PEP 8 too, even if it irritates me a bit to use 4-space indents instead of 8-space indents, and not being able to indent by just hitting tab makes life a little bit more annoying in vim and so on (perhaps this will cause me to configure vim for intelligent autoindenting). Go and gofmt has already taught me the useful lesson that I don't care that much any more and that at a certain point it's easier to shrug and go with the standard indentation rather than cling desperately to my own version that I think looks nicer.

(At the same time I haven't actually done very much to change my style, so I'm still sort of vacillating. I did re-indent a recent Github code drop from my original 8-space style to a PEP 8 one before publishing it.)

I will probably be working in code bases written in my 8-space style for years to come; for various reasons I have only a moderate amount of enthusiasm for reformatting existing code, especially if I'm otherwise only touching it lightly for some small modification. But I sort of hope that I can get myself to write all future code in a PEP 8 conformant style. A good first step for this would be figuring out how to do it in GNU Emacs.

(I have conflicting thoughts about existing code bases, as part of me wants to make a clean break with my old style by reformatting almost everything. One of the reasons I'm very hesitant on that is that I'm not at all sure what the best way to do that would be.)

python/ShiftingIndentationStyle written at 00:24:28; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.