Search code examples
pythonindentationpep8

Why does Python pep-8 strongly recommend spaces over tabs for indentation?


I see on Stack Overflow and PEP 8 that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.

Is there an underlying reason for spaces to be preferred? I would have thought that tabs were far easier to work with.


Solution

  • The answer was given right there in the PEP [ed: this passage has been edited out in 2013]. I quote:

    The most popular way of indenting Python is with spaces only.

    What other underlying reason do you need?

    To put it less bluntly: Consider also the scope of the PEP as stated in the very first paragraph:

    This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.

    The intention is to make all code that goes in the official python distribution consistently formatted (I hope we can agree that this is universally a Good Thing™).

    Since the decision between spaces and tabs for an individual programmer is a) really a matter of taste and b) easily dealt with by technical means (editors, conversion scripts, etc.), there is a clear way to end all discussion: choose one.

    Guido was the one to choose. He didn't even have to give a reason, but he still did by referring to empirical data.

    For all other purposes you can either take this PEP as a recommendation, or you can ignore it -- your choice, or your team's, or your team leaders.

    But if I may give you one advice: don't mix'em ;-) [ed: Mixing tabs and spaces is no longer an option.]