我已经习惯了编写像这样工作的函数:
def f():
if sunny:
return
#do non-sunny stuff
Run Code Online (Sandbox Code Playgroud)
我试图找出在模块中使用的等效语法.我想做这样的事情:
if sunny:
import tshirt
#do something here to skip the rest of the file
import raincoat
import umbrella
#continue defining the module for non-sunny conditions
Run Code Online (Sandbox Code Playgroud)
我知道我可以把它写成一个,if/else但是缩进模块的其余部分似乎很愚蠢.
我可以将其余的代码移动到一个单独的模块中并有条件地导入它,但这看起来很痛苦.
考虑到这从一开始就是一件奇怪的事情,单独的文件和额外的缩进可能是合理的。
根据您的实际需要,您可以继续处理所有模块主体,然后在稍后的某个时候删除任何不合适的内容。
def foo():
print "foo"
def bar():
print "bar"
if sunny:
del foo
else:
del bar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2287 次 |
| 最近记录: |