简体   繁体   English

在 Python 中使用导入模块编写自己的函数的最佳实践?

[英]Best practice for writing own functions with imported modules in Python?

I am trying to write my own functions/methods in a tools.py module for an ongoing project.我正在尝试在 tools.py 模块中为正在进行的项目编写自己的函数/方法。

I need to import some modules like numpy for these methods, but am unsure of the best way to do this.我需要为这些方法导入一些模块,如 numpy,但我不确定执行此操作的最佳方法。 Should I import within each method each time I call that function?每次调用 function 时,我是否应该在每种方法中导入? Or in the tools.py script at the beginning?还是在开头的 tools.py 脚本中? I do not always need all the functions in tools.py, and for example, don't always need numpy in the script where I call import tools .我并不总是需要 tools.py 中的所有功能,例如,在我调用import tools的脚本中并不总是需要 numpy 。 I would like my code to be as efficient as possible.我希望我的代码尽可能高效。

I only found info that I need not import numpy if I do not call it directly in a specific script.如果我不直接在特定脚本中调用它,我只发现不需要导入 numpy 的信息。

I could be wrong, but I'm pretty sure importing a module loads the entire thing whether you use import sys or from sys import argv .我可能是错的,但我很确定无论您使用import sys还是from sys import argv导入模块都会加载整个内容。 So, importing numpy into the tools module will make the entire numpy module available to the tools module and, though not directly callable, wherever the tools module is imported into.因此,将numpy导入tools模块将使整个 numpy 模块可用于工具模块,尽管不能直接调用,但无论工具模块导入到何处。 I've only seen imports within a function to achieve optional library support.我只在 function 中看到导入以实现可选的库支持。 I hadn't heard of it being done for the sake of efficiency nor performance.我没有听说为了效率或性能而这样做。

These links may help further:这些链接可能会有所帮助:

Should import statements always be at the top of a module? import 语句是否应该始终位于模块的顶部? https://softwareengineering.stackexchange.com/q/187403 https://softwareengineering.stackexchange.com/q/187403

You shouldn't by importing numpy at every method call.您不应该在每次方法调用时都导入 numpy 。 This should only be done once at the start of your script.这应该只在脚本开始时执行一次。 If you don't want to import the entire module in tools.py, just import specific submodules you need from the library:如果您不想在 tools.py 中导入整个模块,只需从库中导入您需要的特定子模块:

from numpy import submodule1, submodule2

You could allways import only specific functions or classes of a module, ie:您总是可以只导入模块的特定函数或类,即:

from tools import myfunction

Further it is correct that you dont import numpy if you dont need it.此外,如果您不需要它,则不要导入 numpy 是正确的。 However as far as my experience goes, imports of imports are not available in the current script.但是,就我的经验而言,当前脚本中不提供导入的导入。

This means when your tools.py contains the import numpy, and you load your tools in another script main.py , numpy (ie numpy.array() ) wont be available in main.py .这意味着当您的tools.py包含导入 numpy,并且您在另一个脚本main.py中加载您的工具时,numpy (即numpy.array() )将不会在main.py中可用You would need to import it there as well.您还需要将其导入那里。

However you can also import modules inside of functions to reduce the visibility and initial start up time of a script, see also this link但是,您也可以在函数内部导入模块以减少脚本的可见性和初始启动时间,另请参阅此链接

Some general performance tips are also provided here此处还提供了一些一般性能提示

This said, in general modern computers have evolved so far that imo in most use cases you dont need to worry to much about performance这就是说,总的来说,现代计算机已经发展到现在,在大多数用例中,imo 不需要太担心性能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM