Why is “import *” bad?

后端 未结 12 2238
北恋
北恋 2020-11-21 06:26

It is recommended to not to use import * in Python.

Can anyone please share the reason for that, so that I can avoid it doing next time?

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:29

    Understood the valid points people put here. However, I do have one argument that, sometimes, "star import" may not always be a bad practice:

    • When I want to structure my code in such a way that all the constants go to a module called const.py:
      • If I do import const, then for every constant, I have to refer it as const.SOMETHING, which is probably not the most convenient way.
      • If I do from const import SOMETHING_A, SOMETHING_B ..., then obviously it's way too verbose and defeats the purpose of the structuring.
      • Thus I feel in this case, doing a from const import * may be a better choice.

提交回复
热议问题