gpt4 book ai didi

python - `if` 和 `def` 下的缩进代码在 Python 中都称为 block 吗?

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:14 25 4
gpt4 key购买 nike

在 Python 中,keywordsdefclass 引入一个新的 scope .其他的,如 iffor 则不这样做——它们使用封闭代码的范围。 (范围解析在 Short Description of the Scoping Rules? 中解释。)

这两个不同类型的关键字“缩进”的代码行的正确术语是什么?

例子:

def foo():
do_bar() # indent type 1
do_another_bar() # indent type 1

示例 2:

if True:
do_something() # indent type 2
do_more_things() # indent type 2

缩进“类型 1”和“类型 2”都称为代码的“ block ”吗?

最佳答案

一般来说,是的,在 <keyword-clause>: 下缩进的任何文本部分称为“ block ”。

对于 C/C++/Java/JavaScript/Perl/PHP/等中花括号内的任何文本部分也是如此。缩进是 Python 的大括号。

关于python - `if` 和 `def` 下的缩进代码在 Python 中都称为 block 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com