简体   繁体   English

嵌套Python模块导入的最佳实践

[英]Best practice for nested Python module imports

Suppose I have a Python module "main.py": 假设我有一个Python模块“main.py”:

import math           # from the standard Python library
import my_own_module

...

foo = math.cos(bar)

And I also need to import the standard math module in "my_own_module.py": 我还需要在“my_own_module.py”中导入标准数学模块:

import math

...

baz = math.sin(qux)

In this case I think import math in "main.py" is redundant and can be omitted. 在这种情况下,我认为“main.py”中的import math是多余的,可以省略。

What's best practice in this case: 在这种情况下,最佳做法是什么:

  1. Omit import math from "main.py" becuase it's redundant? 省略“main.py”中的import math ,因为它是多余的? Or, 要么,
  2. Keep import math in "main.py" to clarify that the code in that module requires it? import math保存在“main.py”中以澄清该模块中的代码是否需要它?

The reference to math.cos in main.py means that import math is required in main.py , regardless of whether my_own_module.py imports it or not. 到基准math.cosmain.py意味着import math是必需的main.py ,无论my_own_module.py进口与否。 It is not redundant, and it cannot be omitted (and if you try to omit it, you'll get an error). 它不是多余的,也不能省略(如果你试图省略它,你会得到一个错误)。

import math

does something else than simply including the full text of one file into the other . 做一些事情不是简单地包括一个文件的全文为其他

It introduces a new namespace with the name math , and this math name will be known in your current namespace . 它引入了一个名为math的新命名空间 ,这个math名称将在您当前的命名空间中知道。

If you omit the 如果你省略了

import math

from your main.py file, your command 从你的main.py文件,你的命令

foo = math.cos(bar)

becomes illegal , as the math symbol will be not (recognized) in the main.py namespace. 变得非法 ,因为在main.py命名空间中不会(识别) math符号。

This is not like, eg #include in C++. 这不是,例如C ++中的#include The import is not optional. 导入不是可选的。 Importing a module is required to be able to refer to its contents. 导入模块需要能够引用其内容。 This is true for every single file that does it. 这对于每个执行此操作的文件都是如此。

A good question. 一个好问题。 The short answer is yes, if you use a math function in a py file then you need to import the module at the top regardless of how many times its imported elsewhere. 简短的回答是肯定的,如果你在py文件中使用数学函数,那么无论在别处导入多少次,你都需要在顶部导入模块。

It gets interesting when we throw a thrid file into the mix, lets call this "explanation.py" 当我们将一个thrid文件放入混合时,它会变得有趣,我们称之为“explain.py”

And lets suppose that your "main.py" becomes "my_functions.py" and contains a function called foo: 假设您的“main.py”变为“my_functions.py”并包含一个名为foo的函数:

#my_functions.py
import math
import my_own_module
def foo(bar):
    return math.cos(bar)

and in my_own_module.py: 在my_own_module.py中:

#my_own_module.py
import math
def bar(foo):
    return math.sin(foo)

and finally explanation.py (new main()) 最后explain.py(new main())

#main.py
import my_functions
import my_own_module
bar = my_functions.foo(10)
foo =  my_own_module.bar(10)
print(foo)
print(bar)

Notice how you DO NOT need to add math if you call the functions imported from another file. 请注意,如果调用从其他文件导入的函数,则不需要添加数学。 I hope that might add further clarity to your enquiry :) 我希望这可以进一步明确你的询问:)

However it might be worth noting that this would exclude maths from the current namespace, therefore rendering any further calls to the math functions useless. 但是,值得注意的是,这将从当前命名空间中排除数学,因此对数学函数的任何进一步调用都是无用的。

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

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