简体   繁体   English

导入模块/功能的方法有哪些?

[英]What are the ways to import a module/function?

It is a very basic question, but I have not seen any explanation covering all possible cases so far. 这是一个非常基本的问题,但到目前为止我还没有看到任何解释所有可能情况的解释。

Suppose m1, m2, m3 are module hierarchies, and fun() is a function inside the hierarchy. 假设m1, m2, m3是模块层次结构,而fun()是层次结构内的函数。

I have seen commands like 我见过像这样的命令

Version 1 版本1

 from m1.m2.m3 import fun fun() #To access the function 

Version 2 版本2

 import m1 m1.m2.m3.fun() 

Are they exactly equivalent to 它们完全相同吗?

Version 3 版本3

 import m1.m2 m1.m2.m3.fun() 

or 要么

Version 4 版本4

 from m1.m2 import m3 m3.fun() 

Or any other combinations in-between? 或者其他任何组合? Is there any relative advantage or disadvantage? 有任何相对优势或劣势吗? Clearly, I would rather like to write fun() while calling the function each time rather than writing m1.m2.m3.fun() but what is the trade off? 显然,我更愿意在每次调用函数时编写fun()而不是编写m1.m2.m3.fun()但是什么是权衡?
From my understanding, version 2 will execute the whole script of m1. 根据我的理解,版本2将执行m1的整个脚本。 But are the others more selective in their execution (and hence possibly get to the __main__ quicker?) 但是其他人在执行时是否更具选择性(因此可能更快地进入__main__ ?)

one other way of importing is 另一种导入方式是

import m1.m2.m3 as m

and hence u can call the function using 因此你可以调用函数

m.fun()

the most import thing to be careful about is not to override something that already exists ( polluting the namespace ) 最重要的一点是不要覆盖已存在的东西(污染命名空间)

Version 1 版本1

 from m1.m2.m3 import fun fun() #To access the function 

Version 1 is the one to go with if fun is the only function you want from the m1 tree and you can guarantee that no other function inside your script will ever be called fun . 版本1是一个去与如果 fun是你在想的唯一功能m1你能保证你的脚本中没有其他的功能都不会被调用fun If any of the above are not satisfied do not do it , but if the are, do it. 如果上述任何一项不满意, 请不要这样做 ,但如果是, 那就去做吧。


Version 2 版本2

 import m1 m1.m2.m3.fun() 

Version 2 is the one to go with if you want to be able to access everything inside m1 . 如果您希望能够访问m1内的所有内容可以使用版本2。 It does lead to more verbose code because you have to carry the m1 with you all the time but it is safer and far less obscure that the equivalent (which should be discouraged) from m1 import * . 它确实会导致更冗长的代码,因为你必须随身携带m1 ,但它更安全,而且不那么模糊,因为它from m1 import *相当(应该不鼓励)。 Note that m1 code will be executed. 请注意,将执行m1代码。


Version 3 版本3

 import m1.m2 m1.m2.m3.fun() 

Version 3 is the one to go with if you want to be able to access everything inside m2 but not higher (cannot do m1.more_fun() ). 第3版是一个一起去,如果你想能够访问内部的一切 m2但不高于(不能做m1.more_fun() Note that m2 code will be executed. 请注意, m2代码将被执行。


Version 4 版本4

 from m1.m2 import m3 m3.fun() 

Version 4 is the one to go with if you want to be able to access everything inside m3 but not higher (cannot do m1.m2.more_fun() ). 第4版是一个一起去,如果你想能够访问内部的一切 m3 ,但不高于(不能做m1.m2.more_fun() Note that m3 code will be executed. 请注意,将执行m3代码。

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

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