在 python 中导入模块时是否有类似 `return` 的东西?
Is there an analog to `return` when importing a module in python?
免责声明
我知道,我可以简单地在 if __name__ == '__main__
下将代码缩进 或将其放在函数 中。但是,这会增加不必要的缩进级别。我也知道这会是一种糟糕的风格,我应该编写一个合适的模块。我只是想知道。与 setuptools
一起使用的入口点当然更好,人们不应该懒惰并将这些与 virtualenv 一起用于测试。我问这个问题是为了 'is there a command to finish importing a file early'.
的纯学术知识
真题
可以 return 提早执行函数以防止其余函数代码的执行。 这有利于节省缩进级别。示例:
def my_func(arg):
print arg
if arg == 'barrier':
return
print 'arg is not barrier'
# ...
# alot more nested stuff that
# will not be executed if arg='barrier'
保存缩进级别,与以下相比:
def my_func(arg):
print arg
if arg == 'barrier':
return
else:
print 'arg is not barrier'
# ...
# alot more nested stuff that
# is one indentation level deeper :-(
是否可以在导入模块时做类似的事情,以便不导入其余代码?
例如在模块文件中:
# !/usr/bin/python
# I'm a module and a script. My name is 'my_module.py'.
# I define a few functions for general use, and can be run standalone.
def my_func1(arg):
# ... some code stuff ...
pass
def my_func2(gra):
# ... some other useful stuff ...
pass
if __name__ != '__main__':
importreturn # Statement I'm looking for that stops importing
# `exit(0)` here would exit the interpreter.
print 'I am only printed when running standalone, not when I'm imported!'
# ... stuff only run when this file is executed by itself ...
所以我看不到 "I am only printed when running standalone, not when I'm imported!",当我看到时:
import my_module.py
my_func2('foobar')
您不想避免缩进,缩进简明扼要地说明了您打算代码执行的操作:
if __name__ == '__main__':
print "I am only printed when running standalone, not when I'm imported!"
else:
print "I am only printed when importing"
如果您愿意,可以将其包装在一个函数中吗?
def main():
if __name__ == '__main__':
print "I am only printed when running standalone, not when I'm imported!"
return
print "I am only printed when importing"
main()
不,Python中没有相关声明。
最接近您想要的是引发异常并在 try/except 块中进行导入。 raise语句后的代码不会被执行
要真正实现这样的语句,您需要一个自定义导入处理程序来进行一些预处理,and/or 一个自定义编译器。如果您想了解 Python 内部结构,这可能是一个有趣的挑战,但不是您应该在实际应用程序中使用的东西。
免责声明
我知道,我可以简单地在 if __name__ == '__main__
下将代码缩进 或将其放在函数 中。但是,这会增加不必要的缩进级别。我也知道这会是一种糟糕的风格,我应该编写一个合适的模块。我只是想知道。与 setuptools
一起使用的入口点当然更好,人们不应该懒惰并将这些与 virtualenv 一起用于测试。我问这个问题是为了 'is there a command to finish importing a file early'.
真题
可以 return 提早执行函数以防止其余函数代码的执行。 这有利于节省缩进级别。示例:
def my_func(arg):
print arg
if arg == 'barrier':
return
print 'arg is not barrier'
# ...
# alot more nested stuff that
# will not be executed if arg='barrier'
保存缩进级别,与以下相比:
def my_func(arg):
print arg
if arg == 'barrier':
return
else:
print 'arg is not barrier'
# ...
# alot more nested stuff that
# is one indentation level deeper :-(
是否可以在导入模块时做类似的事情,以便不导入其余代码? 例如在模块文件中:
# !/usr/bin/python
# I'm a module and a script. My name is 'my_module.py'.
# I define a few functions for general use, and can be run standalone.
def my_func1(arg):
# ... some code stuff ...
pass
def my_func2(gra):
# ... some other useful stuff ...
pass
if __name__ != '__main__':
importreturn # Statement I'm looking for that stops importing
# `exit(0)` here would exit the interpreter.
print 'I am only printed when running standalone, not when I'm imported!'
# ... stuff only run when this file is executed by itself ...
所以我看不到 "I am only printed when running standalone, not when I'm imported!",当我看到时:
import my_module.py
my_func2('foobar')
您不想避免缩进,缩进简明扼要地说明了您打算代码执行的操作:
if __name__ == '__main__':
print "I am only printed when running standalone, not when I'm imported!"
else:
print "I am only printed when importing"
如果您愿意,可以将其包装在一个函数中吗?
def main():
if __name__ == '__main__':
print "I am only printed when running standalone, not when I'm imported!"
return
print "I am only printed when importing"
main()
不,Python中没有相关声明。
最接近您想要的是引发异常并在 try/except 块中进行导入。 raise语句后的代码不会被执行
要真正实现这样的语句,您需要一个自定义导入处理程序来进行一些预处理,and/or 一个自定义编译器。如果您想了解 Python 内部结构,这可能是一个有趣的挑战,但不是您应该在实际应用程序中使用的东西。