Python 自动导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1493888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:24:20  来源:igfitidea点击:

Python Auto Importing

python

提问by Nodren

Possible Duplicate:
Perl's AUTOLOAD in Python (getattron a module)

可能的重复:
Python 中的 Perl 自动加载(模块上的getattr

I'm coming from a PHP background and attempting to learn Python, and I want to be sure to do things the "Python way" instead of how i've developed before.

我来自 PHP 背景并试图学习 Python,我想确保以“Python 方式”而不是我以前开发的方式做事。

My question comes from the fact in PHP5 you can set up your code so if you attempt to call a class that doesn't exist in the namespace, a function will run first that will load the class in and allow you to continue on as if it were already loaded. the advantages to this is classes weren't loaded unless they were called, and you didn't have to worry about loading classes before using them.

我的问题来自这样一个事实,您可以在 PHP5 中设置代码,因此如果您尝试调用命名空间中不存在的类,则会首先运行一个函数,该函数将加载该类并允许您继续执行,就好像它已经加载了。这样做的优点是除非调用类,否则不会加载它们,并且您不必担心在使用它们之前加载类。

In python, there's alot of emphasis on the import statement, is it bad practice to attempt an auto importing trick with python, to alleviate the need for an import statement? I've found this modulethat offers auto importing, however I dont know if that's the best way of doing it, or if auto importing of modules is something that is recommended, thoughts?

在python中,有很多重点放在import语句上,尝试使用python进行自动导入技巧以减轻对import语句的需求是不好的做法吗?我发现这个模块提供自动导入,但是我不知道这是否是最好的方法,或者是否推荐自动导入模块,想法?

采纳答案by dcrosta

Imports serve at least two other important purposes besides making the modules or contents of the modules available:

除了使模块或模块的内容可用之外,导入至少还有两个其他重要目的:

  1. They serve as a sort of declaration of intent -- "this module uses services from this other module" or "this module uses services belonging to a certain class" -- e.g. if you are doing a security review for socket-handling code, you can begin by only looking at modules that import socket(or other networking-related modules)
  2. Imports serve as a proxy for the complexity of a module. If you find yourself with dozens of lines of imports, it may be time to reconsider your separation of concerns within the module, or within your application as a whole. This is also a good reason to avoid "from foo import *"-type imports.
  1. 它们作为一种意图声明——“这个模块使用来自另一个模块的服务”或“这个模块使用属于某个类的服务”——例如,如果您正在对套接字处理代码进行安全,您可以从只查看导入的模块socket(或其他与网络相关的模块)开始
  2. 导入充当模块复杂性的代理。如果您发现自己有几十行导入,那么可能是时候重新考虑在模块内或整个应用程序内的关注点分离了。这也是避免“ from foo import *”类型导入的一个很好的理由。

回答by Martin v. L?wis

In Python, people usually avoid auto imports, just because it is not worth the effort. You may slightly remove startup costs, but otherwise, there is no (or should be no) significant effect. If you have modules that are expensive to import and do a lot of stuff that doesn't need to be done, rather rewrite the module than delay importing it.

在 Python 中,人们通常避免自动导入,只是因为它不值得付出努力。您可以稍微去除启动成本,但除此之外,没有(或应该没有)显着影响。如果您有导入成本高昂的模块并且做了很多不需要完成的事情,与其延迟导入,不如重写模块。

That said, there is nothing inherently wrong with auto imports. Because of the proxy nature, there may be some pitfalls (e.g. when looking at a thing that has not been imported yet). Several auto importing libraries are floating around.

也就是说,汽车进口并没有本质上的错误。由于代理性质,可能存在一些陷阱(例如,在查看尚未导入的事物时)。有几个自动导入库在浮动。

回答by Ned Batchelder

If you are learning Python and want to do things the Python way, then just import the modules. It's veryunusual to find autoimports in Python code.

如果您正在学习 Python 并希望以 Python 的方式做事,那么只需导入模块。在 Python 代码中找到自动导入是非常不寻常的。

回答by linkmaster03

You could auto-import the modules, but the most I have ever needed to import was about 10, and that is after I tacked features on top of the original program. You won't be importing a lot, and the names are very easy to remember.

您可以自动导入模块,但我最多需要导入大约 10 个模块,这是在我在原始程序之上添加功能之后。您不会大量导入,而且名称很容易记住。