简体   繁体   English

“从x导入y为z”与“import xy as z”

[英]“from x import y as z” vs. “import x.y as z”

I'm assuming they are functionally the same, bar some negligible under-the-hood differences. 我假设它们在功能上是相同的,可以忽略一些可以忽略不计的引擎盖差异。 If so, which form is more Pythonic? 如果是这样,哪种形式更像Pythonic?

The xy form makes it implicit that packages and modules are involved, and should be the preferred form when that is the case. xy表单隐含包含包和模块,在这种情况下应该是首选形式。

If t is a symbol defined in module y , then: 如果t是模块y定义的符号,则:

>>> from x.y import t as z
>>>

... but ! ...... 但是

>>> import x.y.t as z
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named t
>>> 

The dot notation is reserved for modules, and should be used when modules are involved. 点符号是为模块保留的,应在涉及模块时使用。

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

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