Python 是否有推荐的多行导入格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14376900/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:14:09  来源:igfitidea点击:

Is there a recommended format for multi-line imports?

pythonpython-2.7pep8

提问by Manuel Alvarez

I have read there are three ways for coding multi-line imports in python

我已经阅读了在 python 中编码多行导入的三种方法

With slashes:

带斜线:

from Tkinter import Tk, Frame, Button, Entry, Canvas, Text, \
    LEFT, DISABLED, NORMAL, RIDGE, END

Duplicating senteces:

复制句子:

from Tkinter import Tk, Frame, Button, Entry, Canvas, Text
from Tkinter import LEFT, DISABLED, NORMAL, RIDGE, END

With parenthesis:

带括号:

from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
    LEFT, DISABLED, NORMAL, RIDGE, END)

Is there a recomended format or a more elegant way for this statements?

对于此语句,是否有推荐的格式或更优雅的方式?

采纳答案by Brendan Maguire

Personally I go with parentheses when importing more than one component and sort them alphabetically. Like so:

我个人在导入多个组件时使用括号并按字母顺序对它们进行排序。像这样:

from Tkinter import (
    Button,
    Canvas,
    DISABLED,
    END,
    Entry,
    Frame,
    LEFT,
    NORMAL,
    RIDGE,
    Text,
    Tk,
)

This has the added advantage of easily seeing what components have been added / removed in each commit or PR.

这有一个额外的优势,可以轻松查看在每个提交或 PR 中添加/删除了哪些组件。

Overall though it's a personal preference and I would advise you to go with whatever looks best to you.

总的来说,虽然这是个人喜好,但我建议您选择最适合您的。

回答by poke

Usually with Tkinter, it is okay to just use from Tkinter import *as the module will only export names that are clearly widgets.

通常使用 Tkinter,可以直接使用,from Tkinter import *因为该模块只会导出明显是小部件的名称。

PEP 8does not list any conventions for such a case, so I guess it is up to you to decide what is the best option. It is all about readability, so choose whatever makes it clear that you are importing stuff from a single module.

PEP 8没有列出这种情况的任何约定,所以我想由你来决定什么是最好的选择。一切都与可读性有关,因此请选择任何明确表示您正在从单个模块导入内容的内容。

As all those names are made available in your scope, I personally think that options 2 is the most clearest as you can see the imported names the best. You then could even split it up more to maybe group those names together that belong with each other. In your example I might put Tk, Frameand Canvasseparately as they group widgets together, while having Buttonand Textseparately as they are smaller components in a view.

由于所有这些名称都在您的范围内可用,我个人认为选项 2 是最清晰的,因为您可以最好地看到导入的名称。然后,您甚至可以将其拆分更多,以便将这些属于彼此的名称组合在一起。在您的示例中,我可能会将Tk,FrameCanvas分开放置,因为它们将小部件组合在一起,而将ButtonText分开放置,因为它们是视图中的较小组件。

回答by Thorsten Kranz

Your examples seem to stem from PEP 328. There, the parenthesis-notation is proposed for exactly this problem, so probably I'd choose this one.

您的示例似乎源于PEP 328。在那里,括号表示法正是针对这个问题提出的,所以我可能会选择这个。

回答by Max Malysh

I would go with the parenthesis notation from the PEP328with newlines added before and after parentheses:

我会使用PEP328 中的括号表示法,并在括号前后添加换行符:

from Tkinter import (
    Tk, Frame, Button, Entry, Canvas, Text, 
    LEFT, DISABLED, NORMAL, RIDGE, END
)

This is the format which Djangouses:

这是Django使用的格式:

from django.test.client import Client, RequestFactory
from django.test.testcases import (
    LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase,
    skipIfDBFeature, skipUnlessAnyDBFeature, skipUnlessDBFeature,
)
from django.test.utils import (
    ignore_warnings, modify_settings, override_settings,
    override_system_checks, tag,
)