Python Module Import: Single-line vs Multi-line

后端 未结 3 1378
天涯浪人
天涯浪人 2020-12-13 08:07

So this is just a simple question. In python when importing modules, what is the difference between this:

from module import a, b, c, d

and

3条回答
  •  情深已故
    2020-12-13 08:54

    To add to some of the questions raised from inspectorG4dget's answer, you can also use tuples to do multi-line imports when folder structures start getting deeply nested or you have modules with obtuse names.

    from some.module.submodule.that_has_long_names import (
        first_item,
        second_item,
        more_imported_items_with_really_enormously_long_names_that_might_be_too_descriptive,
        that_would_certainly_not_fit,
        on_one_line,
    )
    

    This also works, though I'm not a fan of this style:

    from module import (a_ton, of, modules, that_seem, to_keep, needing,
                        to_be, added, to_the_list, of_required_items)
    

提交回复
热议问题