如何避免 Python 导入中的行继续

How to avoid line continuations in Python imports

我有许多 类 / 函数要从模块中导入,而 linters / 样式检查器(pylintflakepep8)抱怨说行太长,我被迫使用丑陋的行延续:

from my_lengthy_module import FirstClass, SecondClass, ThirdClass, \  
foo_bar_with_long_name, bar_foo_with_longer_name, \
FourthClass, bar_foo_with_longer_name, foo_bar_with_longest_name

如何做得更好?

Python 2.5 introduced a concept of multi-line imports (PEP-328) 通过扩展 import 语句的语法以将导入的名称包含在括号中从而避免行继续来解决此问题:

from my_lengthy_module import (
    FirstClass, SecondClass, ThirdClass, 
    foo_bar_with_long_name, bar_foo_with_longer_name,
    FourthClass, bar_foo_with_longer_name, foo_bar_with_longest_name
)