How to avoid line continuations in Python imports

橙三吉。 提交于 2019-12-13 03:35:30

问题


I have a number of classes / functions to import from a module and linters/ style checkers (pylint, flake, pep8) are complaining that the line is too long and I am forced to use line continuation which is ugly:

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

How to do it better?


回答1:


Python 2.5 introduced a concept of multi-line imports (PEP-328) which address this problem by extending the syntax of the import statement to include the imported names in brackets and thus avoiding line continuations:

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
)


来源:https://stackoverflow.com/questions/54314661/how-to-avoid-line-continuations-in-python-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!