在Python中长进口 [英] Long imports in Python

查看:129
本文介绍了在Python中长进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很少必须从blpblq.lqlqlqlq.bla中输入类似

I seldom have to write something like

from blqblq.lqlqlqlq.bla import fobarbazbarbarbazar as foo
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

这需要超过80个字符。 官方Python编码风格指南中未介绍这种情况。我如何用pythonically编写这样的导入?

which takes more than 80 characters. This situation is not covered in the official Python coding style guide. How do I write such imports pythonically?

推荐答案

http://www.python.org/dev/peps/pep-0008/#maximum-line-length


包裹长行的首选方法是使用Python的括号,括号和大括号中的
行继续。长行
可以通过在
括号中包装表达式来分割多行。应该优先使用反斜杠
用于行连续。确保适当地缩进连续行

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

因此,在这种情况下,这个二进制运算符
的优先位置在运算符之后,而不在它之前。可以是:

So in your case this could be:

from blqblq.lqlqlqlq.bla import (
                                 fobarbazbarbarbazar
                                 as foo)
from matplotlib.backends.backend_qt4agg import (
                                                 FigureCanvasQTAgg
                                                 as FigureCanvas)

我个人总是使用这种风格,我发现更长的线条可读性:

Personally I always use this style which I find more readable with long lines:

# Just 1 indent
from blqblq.lqlqlqlq.bla import (
    fobarbazbarbarbazar
    as foo
) # end at the next line so it's always clear where what ends

from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas
)

这篇关于在Python中长进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆