简体   繁体   English

关于 Python 命名空间和 function 导入的问题:是否最好只从模块中导入您需要的 function?

[英]Question regarding Python namespaces and function imports: Is it preferable to import only the function you need from a module?

So I don't recall where I heard this or read this, but I remember being instructed that it was best practice to import only the needed functions or classes from modules for several reasons.所以我不记得我是在哪里听到或读到这个的,但我记得有人告诉我,出于多种原因,最好只从模块中导入所需的函数或类。

We should do this我们应该这样做

from my_module import (needed_function, NeededClass)
from numpy import (arange, nditer, ones, zeros, array ndarray)

needed_function()
NeededClass()

ones
arange(0,16).reshape(4,4)
#etc etc

rather than import the module and calling the needed functions而不是导入模块并调用所需的函数

import my_module as mm
import numpy as np

mm.needed_function()
mm.NeededClass()

np.ones
np.arange(0,16).reshape(4,4)
#etc etc

I was told that the primary reason is that when module.function is called Python must search the local namespace for module then search the module's namespace for function , then continue.有人告诉我,主要原因是当module.function被调用时 Python 必须在本地命名空间中搜索module ,然后在模块的命名空间中搜索function ,然后继续。 This must be done each time module.function is called and is potentially very time wasting (for large computations).这必须在每次调用module.function时完成,并且可能非常浪费时间(对于大型计算)。

The contrasts with directly importing the function because then python only needs to search the local namespace once for that function, a much smaller task than searching a potentially very large namespace, such as numpy or scipy.与直接导入 function 形成对比,因为 python 只需要为该 function 搜索本地命名空间一次,这比搜索可能非常大的命名空间(例如 numpy 或 scipy)要小得多。

Is this correct?这样对吗?

Some good answers: https://softwareengineering.stackexchange.com/questions/187403/import-module-vs-from-module-import-function一些好的答案: https://softwareengineering.stackexchange.com/questions/187403/import-module-vs-from-module-import-function

Personally for my use case, I'm creating an.exe that needs to be very portable and as small file size as possible.就我个人的用例而言,我正在创建一个 .exe,它需要非常便携并且文件大小尽可能小。 Because of that, I mostly import only single functions where possible.因此,我大多只在可能的情况下导入单个函数。

暂无
暂无

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

相关问题 Python 导入系统,从一个模块导入 function,从另一个模块导入相同的 function - Python Import system, importing function from a module that imports the same function from another package 从导入另一个模块的类的模块导入函数时的Python导入错误(ModuleNotFoundError) - Python Import Error (ModuleNotFoundError) when importing a function from a module that imports a class from another module 关于 Python 中的 Lambda function 的问题 - A question regarding the Lambda function in Python 使用 Python 3 从位于另一个目录中的模块导入本地 function 并在 Jupyter Notebook 中进行相对导入 - Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3 无法从“module.py”导入名称“function” - 同一目录,来自同一模块的另一个函数导入 - Cannot import name 'function' from 'module.py' - same directory, another function from the same module imports 关于 python 模块并从中调用 function 的问题 - Question about python module and calling a function from it “ from module import function”语句是否导致给定模块中其他函数的隐式导入 - Does 'from module import function' statement cause implicit imports of other functions in given module python动态从模块导入功能按字符串 - python dynamic from module import function by string Python:从已导入的模块导入功能 - Python: import function from an already imported module 无法从 python 中的模块导入异步 function - Can not import async function from module in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM