【发布时间】:2022-08-17 22:30:40
【问题描述】:
我在 Python 中找到了这段代码,它运行良好。 但是这个 \"if True:\" 的目的是什么,因为 if 总是 True 所以里面的代码每次都会运行。你知道我们可以在哪些情况下使用它吗?
if True:
print (type(0x1F))
print (0b010101010)
print (0o7)
标签: python-3.x
我在 Python 中找到了这段代码,它运行良好。 但是这个 \"if True:\" 的目的是什么,因为 if 总是 True 所以里面的代码每次都会运行。你知道我们可以在哪些情况下使用它吗?
if True:
print (type(0x1F))
print (0b010101010)
print (0o7)
标签: python-3.x
这是在 Python 中引入块作用域的一种方式,相当于 C 语言中的{/*code*/)。
它也可以是一种有用的重构技术。
【讨论】: