为什么Python严格限制缩进? [英] Why is Python strict about indentation?

查看:153
本文介绍了为什么Python严格限制缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题来自此堆栈溢出问题 .为什么Python设计为使用空格或制表符的正确缩进?这样,有目的地服务于任何利益吗?

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?

推荐答案

缩进在Python中是必不可少的.它以类似C的语法替换花括号,分号等.

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?")

如果不是缩进,我们将无法指示第二个print语句所属的代码块.是在if语句下,函数f下还是仅在文档中本身?

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?

这篇关于为什么Python严格限制缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆