这两个进口有什么区别?

时间:2021-05-19 22:28:18

Do the below 2 import statements have some difference? Or just the same thing?

以下2个导入语句有一些区别吗?或者只是同样的事情?

from package import *

import package

2 个解决方案

#1


12  

from package import * imports everything from package into the local namespace; this is not recommended because it may introduce unwanted things (like a function that overwrites a local one). This is a quick and handy import tool, but if things get serious, you should use the from package import X,Y,Z, or import package syntax.

从包导入*导入从包到本地命名空间的所有内容;建议不要这样做,因为它可能会引入不需要的东西(比如覆盖本地的东西)。这是一个快速而方便的导入工具,但如果情况严重,您应该使用from package import X,Y,Z或import package语法。

import package imports everything from package into the local package object. So if package implements the something() function, you will use it by package.something().

import package导入从包到本地包对象的所有内容。因此,如果package实现了something()函数,您将通过package.something()使用它。

Also, another thing that should be talked about is the nested namespace case: suppose you have the function package.blabla.woohoo.func(), you can import package.blabla.woohoo and use package.blabla.woohoo.func(), but that is too complicated. Instead, the easy way to do it is from package.blabla import woohoo and then use woohoo.func() or from package.blabla.woohoo import func, and then use func(). I hope this makes sense. If it doesn't, here's a code piece to illustrate:

另外,应该讨论的另一件事是嵌套的命名空间情况:假设你有函数package.blabla.woohoo.func(),你可以导入package.blabla.woohoo并使用package.blabla.woohoo.func(),但那太复杂了。相反,简单的方法是从package.blabla import woohoo然后使用woohoo.func()或者从package.blabla.woohoo import func,然后使用func()。我希望这是有道理的。如果没有,这里有一个代码片来说明:

import package.blabla.woohoo
package.blabla.woohoo.func()

from package.blabla import woohoo
woohoo.func()

from package.blabla.woohoo import func
func()

Hope this helps :)

希望这可以帮助 :)

#2


3  

The difference is the use of a namespace for the package.

不同之处在于使用包名称空间。

from package import *
class_in_package()

vs

import package
package.class_in_package()

#1


12  

from package import * imports everything from package into the local namespace; this is not recommended because it may introduce unwanted things (like a function that overwrites a local one). This is a quick and handy import tool, but if things get serious, you should use the from package import X,Y,Z, or import package syntax.

从包导入*导入从包到本地命名空间的所有内容;建议不要这样做,因为它可能会引入不需要的东西(比如覆盖本地的东西)。这是一个快速而方便的导入工具,但如果情况严重,您应该使用from package import X,Y,Z或import package语法。

import package imports everything from package into the local package object. So if package implements the something() function, you will use it by package.something().

import package导入从包到本地包对象的所有内容。因此,如果package实现了something()函数,您将通过package.something()使用它。

Also, another thing that should be talked about is the nested namespace case: suppose you have the function package.blabla.woohoo.func(), you can import package.blabla.woohoo and use package.blabla.woohoo.func(), but that is too complicated. Instead, the easy way to do it is from package.blabla import woohoo and then use woohoo.func() or from package.blabla.woohoo import func, and then use func(). I hope this makes sense. If it doesn't, here's a code piece to illustrate:

另外,应该讨论的另一件事是嵌套的命名空间情况:假设你有函数package.blabla.woohoo.func(),你可以导入package.blabla.woohoo并使用package.blabla.woohoo.func(),但那太复杂了。相反,简单的方法是从package.blabla import woohoo然后使用woohoo.func()或者从package.blabla.woohoo import func,然后使用func()。我希望这是有道理的。如果没有,这里有一个代码片来说明:

import package.blabla.woohoo
package.blabla.woohoo.func()

from package.blabla import woohoo
woohoo.func()

from package.blabla.woohoo import func
func()

Hope this helps :)

希望这可以帮助 :)

#2


3  

The difference is the use of a namespace for the package.

不同之处在于使用包名称空间。

from package import *
class_in_package()

vs

import package
package.class_in_package()