python - Python中的长导入

标签 python coding-style

我有时不得不写一些类似的东西

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

需要超过 80 个字符。 official Python coding style guide 中没有涵盖这种情况。 .如何以 Python 方式编写此类导入?

最佳答案

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

The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).

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.

所以在你的情况下,这可能是:

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

就我个人而言,我总是使用这种风格,我觉得长行更易读:

# 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 - Python中的长导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17273847/

相关文章:

jsp - 与 JSP 等效的 Checkstyle?

html - 在 HTML 中居中​​文本

java - 在 Java 中使用匿名类被认为是不好的风格还是好的?

python - 我怎样才能在 Django 中查询 .save()?

python - django queryset values()方法字段顺序与返回的顺序不同

python - 将 Matlab (Octave) 组着色代码翻译成 python (numpy, pyplot)

assembly - g++使用哪种ASM风格

Python fork : 'Cannot allocate memory' if process consumes more than 50% avail. 内存

python - Python 中的 pickle 和 yaml 之间的基本区别是什么?

powershell - PowerShell:一长串条件,优雅地使用逻辑运算符