Python 模块导入:单行 vs 多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15011367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Module Import: Single-line vs Multi-line
提问by Cody Brown
So this is just a simple question. In python when importing modules, what is the difference between this:
所以这只是一个简单的问题。在python中导入模块时,这有什么区别:
from module import a, b, c, d
and this
和这个
from module import a
from module import b
from module import c
from module import d
To me it makes sense always to condense code and use the first example, but I've been seeing some code samples out there dong the second. Is there any difference at all or is it all in the preference of the programmer?
对我来说,压缩代码并使用第一个示例总是有意义的,但我一直在看到一些代码示例。有什么区别还是完全取决于程序员的偏好?
采纳答案by inspectorG4dget
There is no difference at all. They both function exactly the same.
完全没有区别。它们的功能完全相同。
However, from a stylistic perspective, one might be more preferable than the other. And on that note, the PEP-8 for importssays that you should compress from module import name1, name2onto a single line and leave import module1on multiple lines:
但是,从文体的角度来看,一个可能比另一个更可取。在这一点上,用于导入的PEP-8说您应该压缩from module import name1, name2到一行并保留import module1在多行上:
Yes: import os
import sys
No: import sys, os
Ok: from subprocess import Popen, PIPE
In response to @teewuane's comment (repeated here in case the comment gets deleted):
回应@teewuane的评论(在评论被删除的情况下在此处重复):
@inspectorG4dget What if you have to import several functions from one module and it ends up making that line longer than 80 char? I know that the 80 char thing is "when it makes the code more readable" but I am still wondering if there is a more tidy way to do this. And I don't want to do from foo import * even though I am basically importing everything.
@inspectorG4dget 如果您必须从一个模块导入多个函数并且最终使该行长度超过 80 个字符怎么办?我知道 80 个字符是“当它使代码更具可读性时”,但我仍然想知道是否有更整洁的方法来做到这一点。而且我不想从 foo import * 做,即使我基本上是导入所有东西。
The issue here is that doing something like the following could exceed the 80 char limit:
这里的问题是,执行以下操作可能会超出 80 个字符的限制:
from module import func1, func2, func3, func4, func5
To this, I have two responses (I don't see PEP8 being overly clear about this):
对此,我有两个回应(我不认为 PEP8 对此过于明确):
Break it up into two imports:
把它分成两个进口:
from module import func1, func2, func3
from module import func4, func5
Doing this has the disadvantage that if moduleis removed from the codebase or otherwise refactored, then both import lines will need to be deleted. This could prove to be painful
这样做的缺点是,如果module从代码库中删除或以其他方式重构,则需要删除两个导入行。这可能会很痛苦
Split the line:
分割线:
To mitigate the above concern, it may be wiser to do
为了减轻上述担忧,可能更明智的做法是
from module import func1, func2, func3, \
func4, func5
This would result in an error if the second line is not deleted along with the first, while still maintaining the singular import statement
如果第二行没有与第一行一起删除,同时仍然保持单一的导入语句,这将导致错误
回答by Jacob Powers
To add to some of the questions raised from inspectorG4dget's answer, you can also use tuples to do multi-line imports when folder structures start getting deeply nested or you have modules with obtuse names.
为了补充从inspectorG4dget的回答中提出的一些问题,当文件夹结构开始深度嵌套或者您有名称不明确的模块时,您还可以使用元组进行多行导入。
from some.module.submodule.that_has_long_names import (
first_item,
second_item,
more_imported_items_with_really_enormously_long_names_that_might_be_too_descriptive,
that_would_certainly_not_fit,
on_one_line,
)
This also works, though I'm not a fan of this style:
这也有效,尽管我不喜欢这种风格:
from module import (a_ton, of, modules, that_seem, to_keep, needing,
to_be, added, to_the_list, of_required_items)
回答by Shital Shah
I would suggest not to follow PEP-8 blindly. When you have about half screen worth of imports, things start becoming uncomfortable and PEP-8 is then in conflicts with PEP-20 readability guidelines.
我建议不要盲目遵循 PEP-8。当你有大约一半的屏幕导入时,事情开始变得不舒服,然后 PEP-8 与 PEP-20 可读性指南发生冲突。
My preference is,
我的偏好是,
- Put all built-in imports on one line such as sys, os, time etc.
- For other imports, use one line per package (not module)
- 将所有内置导入如 sys、os、time 等放在一行。
- 对于其他导入,每个包使用一行(不是模块)
Above gives you good balance because the reader can still quickly glance the dependencies while achieving reasonable compactness.
上面给了你很好的平衡,因为读者仍然可以快速浏览依赖项,同时实现合理的紧凑性。
For example,
例如,
My Preference
我的偏好
# one line per package
import os, json, time, sys, math
import numpy as np
import torch, torch.nn as nn, torch.autograd, torch.nn.functional as F
from torchvision models, transforms
PEP-8 Recommandation
PEP-8 推荐
# one line per module or from ... import statement
import os
import json
import time
import sys
import math
import numpy as np
import torch
from torch import nn as nn, autograd, nn.functional as F
from torchvision import models, transforms

