gpt4 book ai didi

python - python中源文件的条件评估

转载 作者:行者123 更新时间:2023-11-28 19:40:52 25 4
gpt4 key购买 nike

假设我有一个仅用于预生产代码的文件

我想确保它不会在生产代码中运行 - 对它的任何调用都必须失败。

文件顶部的这个片段不起作用 - 它破坏了 Python 语法,该语法指定 return 必须在函数中发生。

if not __debug__:
return None

这里最好的解决方案是什么 - 即不涉及制作巨大的其他解决方案。 :-)

最佳答案

if not __debug__:
raise RuntimeError('This module must not be run in production code.')

关于python - python中源文件的条件评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227292/

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