我有许多要从模块中导入的类/函数,而linters /样式检查器(pylint
,flake
,pep8
)抱怨该行太长,我被迫使用丑陋的行延续:
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
如何做得更好?
答案 0 :(得分:2)
Python 2.5引入了多行导入(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
)