是否有可能以类似return的方式结束python模块导入?


问题内容

我想知道是否有一种编写下面的模块代码的方法,而不必在整个模块代码中添加另一个缩进级别。

# module code
if not condition:
    # rest of the module code (big)

我正在寻找这样的东西:

# module code
if condition:
    # here I need something like a `return`
# rest of the module code (big)

注意,我不想抛出异常,导入应该正常通过。


问题答案:

我不知道有什么解决方案,但是我想您可以将所有代码放在一个内部模块中,然后在不满足条件的情况下将其导入。