Python whitespace doesn’t matter

If you have some programming experience then there is one particular feature of Python that is likely to turn you off.

Whitespace matters. Indentation is significant.

This comes as a shock to many people who are used to it being meaningless. Most languages in common use designate code blocks with braces ({ ... }), or in some cases special keywords (e.g. Lua’s do/then ... end). If you want to write your program without any indentation whatsoever then it won’t stop you.

So having to pay attention to whitespace worries people. Puts them off trying Python. Makes them chafe at the thought of using it. Their freedom is being abridged!

This is really weird, since everyone agrees that consistent indentation is a good idea. In fact, it’s about the easiest and most effective thing you can do to make your code legible.

This is why objecting to syntactically meaningful whitespace in Python is a straw-man. All it’s doing is requiring you to not write unreadable and misleading code. In fact, if you have good habits already then you will never notice it. Ever.

(This post is an example of low-hanging fruit for a programming blog. Something to post about early on, for the sake of getting your opinion out there.)

Join the Conversation


  1. Yeah, whitespace put me off Python until I actually tried it. It’s one of those odd ideas that’s bad in theory but good in practice.

  2. It’s bad in theory and it’s abysmal in practice.

    No one codes in a vacuum. Everyone either tries to share their code with others, or tries to use snippets of code that someone else wrote. I have run into more problems trying to copy-paste code snippets while working with Python than I ever have with any other programming language. If your IDE is not capable of RECONSTRUCTING appropriate whitespace, then your language has a deep flaw. Nearly everything treats whitespace differently from everything else and trying to then use that whitespace as actual functional code? It’s an utter nightmare and I hate it.

Leave a comment

Your email address will not be published. Required fields are marked *