简体   繁体   English

Python:从外部模块导入多种方法

[英]Python: Importing multiple methods from external module

With very common Python modules, I find that importing using the from .. import statement greatly increases the readability of my code, since I can reference methods by name without the dot notation. 对于非常普通的Python模块,我发现使用from .. import语句进行from .. import大大提高了代码的可读性,因为我可以按名称引用方法而无需点符号。 However, in some modules, the methods I require are nested differently, eg in os : 但是,在某些模块中,我需要的方法嵌套不同,例如在os

from os.path import join
from os import listdir, getcwd

Why doesn't from os import path.join, listdir, getcwd work? 为什么不from os import path.join, listdir, getcwd起作用? What would be a "pythonic" way to import all the methods I need in a more succinct manner? 以更简洁的方式导入我需要的所有方法的“ pythonic”方式是什么?

The opinion on whether from <module> import <identifier> is Pythonic itself is quite split - it hides away the origin of a method so it's not easy to figure out whence a certain variable/function comes from just by perusing the code. 关于from <module> import <identifier>是否为Pythonic本身的意见是很分歧的-它隐藏了方法的起源,因此仅通过仔细研究代码就很难确定某个变量/函数是从哪里来的。 On the other hand, it reduces verbosity which some people consider Pythonic even tho it's not specifically mandated. 另一方面,它降低了冗长性,即使没有特别要求,有些人也认为它是Pythonic Either way, Pythonic is as elusive term as you're going to get and more often than not it means " the way I think Python code should look like " backed up by several PEPs and obscure mail list posts while conveniently omitting the ones that go against one's notion of Pythonic . 无论哪种方式, Pythonic都是您将要获取的难以理解的术语,并且通常不是意味着“ 我认为Python代码看起来像这样 ”,并由多个PEP支持,并且邮件列表帖子晦涩难懂,同时方便地省略了那些违背Pythonic的观点。

from os import path.join doesn't work because os defines the os.path module (by directly writing to sys.modules of all things), it's not an identifier in the os module itself. from os import path.join不起作用,因为os定义了os.path 模块 (通过直接写入所有内容的sys.modules ),它不是os模块本身的标识符。 path , however, is an identifier in the os module pointing to the os.path module so you can do from os import path or from os.path import join . 但是, pathos模块中指向os.path模块的标识符,因此您可以from os import pathfrom os.path import join

Finally, succinct and Pythonic are not synonyms, in fact PEP 8 for example prescribes using multiple lines for multiple imports even tho you can succinctly write import <module1>, <module2>, <module3> ... . 最后, succinctPythonic不是同义词,实际上,例如PEP 8规定使用多行进行多次导入,即使您可以简洁地编写import <module1>, <module2>, <module3> ... It says that it's OK to import multiple identifiers like that, tho, but keep in mind that os and os.path are two different modules so based on PEP 8 they shouldn't be on the same line and therefore should be written as: 它说可以导入多个这样的标识符,但是,请记住osos.path是两个不同的模块,因此基于PEP 8,它们不应位于同一行,因此应写为:

from os import <identifier_1>, <identifier_2>
from os.path import <identifier_3>, <identifier_4>

Now, I would go as far as claiming that this is Pythonic but it makes the most sense based on PEP 8, at least to me. 现在,我至会声称这是Pythonic,但至少对我而言,基于PEP 8才最有意义。

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

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