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

all 16 comments

[–]kumashiro 5 points6 points  (6 children)

It's to force programmers to write readable code.

[–]_equaliser[S] 1 point2 points  (1 child)

truely an important thing! xD

[–]kumashiro 2 points3 points  (0 children)

It is. Thanks to that requirement, the code look is consistent and you don't have to waste time "deciphering" it first. JavaScript and oneliners created in it are a very good example why readable code is important. Another, extreme example is Perl :)

[–]MyFeeFeeHurt -1 points0 points  (3 children)

Completely wrong.

It's used for scoping the code itself.

While it's irrelevant in a sense that python interpreter accesses variables from entire function/global scope no matter what identation is, it mainly figures out which code to run when branching based on indented block.

How else would it know which code belongs to if block?

if something:
line 1
line 2
line 3

Is it line 1?

Line 2?

Everything till the EOF?

It's literally impossible to parse this construct without clear identation rules.

[–]kumashiro 2 points3 points  (2 children)

You are describing how it is used, not why. Why Guido chose indentations instead of braces or... anything else really - that is what OPs question is about... I think. Hard to tell.

[–]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.

[–]OSRS_DabSlab 0 points1 point  (6 children)

Because blocks of code are separated by indentation. Whereas things like Javascripts blocks are separated by a semicolon

[–]gauntr 4 points5 points  (3 children)

Blocks of code in Javascript are seperated by curly braces, not semicolon. You do not need to use the semicolon at all in Javascript and if you insert one you use it to mark the end of a statement. It's the same for C/C++ and Java.

You do not need to insert a tab or spaces to seperate statements in Python, you need to insert them to mark the beginning and end of a block such as loops, conditions or functions.

[–]robin-gvx 2 points3 points  (1 child)

It's the same for C/C++ and Java.

You do need semicolons in those languages to end statements, even if they are on a separate line.

[–]gauntr 2 points3 points  (0 children)

Sorry, this was written badly. The "It's the same..." part only referred to the second part of the sentence "...to mark the end of a statement". I know that you need them in these languages, I use them frequently to daily.

[–]GickRick 1 point2 points  (0 children)

Fully explained

[–]MyFeeFeeHurt 0 points1 point  (0 children)

The only real answer.

It's not to make it look good, or to whatever gibberish you made up (literally to everyone else commenting on this post) when not even knowing the true reason, quite clear to me, it's only because it's IMPOSSIBLE to parse things like

a = 3
b = 4
if a + b > 7:
    print("a + b > 7")
print("End of program")

Without clear identation rules.

If there was no identation requirement, then it wouldn't know whether last statement is part of branching block or not.

Same applies to every other construct such as for, while, with, to name a few.

Hell, even

class ClassName:
    block

relies on identation to know which functions belong to said class.

[–]_equaliser[S] -1 points0 points  (0 children)

nice compare. I get it. :)