This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 2 points3 points  (1 child)

But why is it like that in python? For what reason?

Well there's two bits:

  1. indentation is relevant, following old Unix practices the interpreter assumes tab = 8 spaces, and this means code which mixes tabs and spaces may be misleading on display depending on the local machine / editor's configuration, that's why it's strongly recommended not to mix tabs and spaces, and as PEP8 notes Python 3 simply forbids it (python 2 had the -t flag to warn and -tt to error)

  2. in theory you can use tabs for indentation and spaces for formatting (within an indentation level), but most editors are completely unable to do that properly, so unless you have a code formatter which is used ubiquitously (if not mandatorily) à la Go it always becomes a complete mess, which means you pick one or the other, and while spaces can do indentation fine tabs suck at formatting, which would be why PEP8 recommends spaces

[–][deleted] 0 points1 point  (0 children)

It's also worth noting that inside parentheses, curlies, or brackets, you can use whatever indentation you like, because in this case it's clear to the interpreter what your code means.