问题
This question is coming from this Stack Overflow question. Why is Python is designed to use correct indentation either space or tab? In this way, is it purposefully to serve any benefit?
回答1:
Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.
Consider this code snippet, for example:
def f(x):
if x == 0:
print("x is zero")
print("where am i?")
If it weren't for indentation, we would have no way of indicating what code block the second print
statement belongs to. Is it under the if
statement, the function f
or just by itself in the document?
回答2:
There is no set width of a tab character, so Python has no way of knowing how many spaces to count a tab as.
来源:https://stackoverflow.com/questions/26329206/why-is-python-strict-about-indentation