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

August 12, 2016

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


Comments on this page:

By Albert at 2016-08-12 05:18:27:

I don't know about other systems, but with vim you can of course keep hitting tab and have the editor automatically convert tabs to spaces, see eg http://vim.wikia.com/wiki/Converting_tabs_to_spaces

By Greg A. Woods at 2016-08-12 14:29:50:

Have you heard of, or do you use, smart-tabs-mode?

https://www.emacswiki.org/emacs/SmartTabs

I really wish more Python people who use Emacs used it. :-)

It works very well for C code, and it claims to work for Python code too, allowing anyone to set their tab width as they desire.

But of course it doesn't solve the fundamental problem that the wider Python community seems to hate tabs.

By cks at 2016-08-15 00:18:51:

I have a long standing view that Ctrl-I in text means something very specific and that I very much object to things that want a different meaning. Smart Tabs is a better implementation of this idea than many (because it actually thinks about one of the issues) but I'm still not interested, and I don't want people writing Python code that I have to read to use it either. If they think that Ctrl-I is two-space indent and I think it's an 8-space indent, it's reasonably likely that the code they write is going to explode off the right side of my terminal windows.

Written on 12 August 2016.
« Systemd has a problem with SATA disks behind port multipliers
What I did to set up a wireless network and what I have left to do »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Fri Aug 12 00:24:28 2016
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.