In Python, is it a good practice to import all attributes with a wildcard?

前端 未结 5 1650
清歌不尽
清歌不尽 2020-12-21 06:48

and why?

Sometimes I need import all the attributes of a module so I use wildcard importing, but one of my Vim scripts(using flake8 as its syntax checker) always giv

5条回答
  •  -上瘾入骨i
    2020-12-21 06:52

    It's generally not a good idea to use from module import *. Wildcard importing leads to namespace pollution; you imported more names than you need and if you accidentally refer to an imported name you may not get the NameError you wanted.

    Also, if a future version of the library added additional names, you could end up masking other names, leading to stranger bugs still:

    from foo import bar
    from spam import *
    

    If you upgrade spam and it now includes a spam.bar it'll replace the foo.bar import in the line above.

提交回复
热议问题