Why is Python strict about indentation? [closed]

試著忘記壹切 提交于 2019-12-13 09:55:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!