Python 中的 Hello World:致命块堆栈溢出?
Hello World in Python: fatal block stack overflow?
我正在尝试在 Python 中实现一个简单的 Hello World 程序。以下代码可以很好地打印“Hello World”:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8]):
print(chr(data[0]), end="")
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == "__main__":
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
main()
...但是当我尝试在末尾添加感叹号时,像这样:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == "__main__":
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
main()
...我得到一个 Fatal Python error: XXX block stack overflow
.
我尝试添加更多 try/except 子句来捕获块堆栈溢出错误,但似乎没有任何效果。块堆栈到底是什么,为什么这么直接的方法行不通?
使用 Python 3.10 我收到的错误消息更多 self-explanatory:
try:
^^^^
SyntaxError: too many statically nested blocks
你钻得太贪心了,太深了。重组代码,使这么多块彼此嵌套。
这是一个known issue that's been "fixed". However, it's more the exact error message, and nesting depth limit, that changed. Python doesn't really intend to support arbitrarily deep static nesting. It would if doing so were trivial, but 。我预计大多数程序员会认为这种深度嵌套是“一场噩梦”,而不是“一种直接的方法”;-)
我只能猜测您正在尝试尝试并理解 Python 中的异常处理。也许这会有所帮助:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
print(chr(data[0]), end="")
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == '__main__':
while True:
try:
main()
# when the *data* list contains only one element
# main() will implicitly return None - i.e., no exception
break
except Exception:
pass
输出:
Hello world!
话虽如此,您的代码可以简化为:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
print(chr(data[0]), end="")
data[0] += data.pop(1)
if __name__ == '__main__':
while True:
try:
main()
except IndexError:
break
我正在尝试在 Python 中实现一个简单的 Hello World 程序。以下代码可以很好地打印“Hello World”:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8]):
print(chr(data[0]), end="")
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == "__main__":
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
main()
...但是当我尝试在末尾添加感叹号时,像这样:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == "__main__":
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
try:
main()
except Exception as e:
main()
...我得到一个 Fatal Python error: XXX block stack overflow
.
我尝试添加更多 try/except 子句来捕获块堆栈溢出错误,但似乎没有任何效果。块堆栈到底是什么,为什么这么直接的方法行不通?
使用 Python 3.10 我收到的错误消息更多 self-explanatory:
try:
^^^^
SyntaxError: too many statically nested blocks
你钻得太贪心了,太深了。重组代码,使这么多块彼此嵌套。
这是一个known issue that's been "fixed". However, it's more the exact error message, and nesting depth limit, that changed. Python doesn't really intend to support arbitrarily deep static nesting. It would if doing so were trivial, but
我只能猜测您正在尝试尝试并理解 Python 中的异常处理。也许这会有所帮助:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
print(chr(data[0]), end="")
if len(data) > 1:
data[0] += data.pop(1)
raise Exception()
if __name__ == '__main__':
while True:
try:
main()
# when the *data* list contains only one element
# main() will implicitly return None - i.e., no exception
break
except Exception:
pass
输出:
Hello world!
话虽如此,您的代码可以简化为:
def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
print(chr(data[0]), end="")
data[0] += data.pop(1)
if __name__ == '__main__':
while True:
try:
main()
except IndexError:
break