Why is “import *” bad?

后端 未结 12 2258
北恋
北恋 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:44

    It is a very BAD practice for two reasons:

    1. Code Readability
    2. Risk of overriding the variables/functions etc

    For point 1: Let's see an example of this:

    from module1 import *
    from module2 import *
    from module3 import *
    
    a = b + c - d
    

    Here, on seeing the code no one will get idea regarding from which module b, c and d actually belongs.

    On the other way, if you do it like:

    #                   v  v  will know that these are from module1
    from module1 import b, c   # way 1
    import module2             # way 2
    
    a = b + c - module2.d
    #            ^ will know it is from module2
    

    It is much cleaner for you, and also the new person joining your team will have better idea.

    For point 2: Let say both module1 and module2 have variable as b. When I do:

    from module1 import *
    from module2 import *
    
    print b  # will print the value from module2
    

    Here the value from module1 is lost. It will be hard to debug why the code is not working even if b is declared in module1 and I have written the code expecting my code to use module1.b

    If you have same variables in different modules, and you do not want to import entire module, you may even do:

    from module1 import b as mod1b
    from module2 import b as mod2b
    
    0 讨论(0)
  • 2020-11-21 06:46

    That is because you are polluting the namespace. You will import all the functions and classes in your own namespace, which may clash with the functions you define yourself.

    Furthermore, I think using a qualified name is more clear for the maintenance task; you see on the code line itself where a function comes from, so you can check out the docs much more easily.

    In module foo:

    def myFunc():
        print 1
    

    In your code:

    from foo import *
    
    def doThis():
        myFunc() # Which myFunc is called?
    
    def myFunc():
        print 2
    
    0 讨论(0)
  • 2020-11-21 06:52

    http://docs.python.org/tutorial/modules.html

    Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code.

    0 讨论(0)
  • 2020-11-21 06:53
    • Because it puts a lot of stuff into your namespace (might shadow some other object from previous import and you won't know about it).

    • Because you don't know exactly what is imported and can't easily find from which module a certain thing was imported (readability).

    • Because you can't use cool tools like pyflakes to statically detect errors in your code.

    0 讨论(0)
  • 2020-11-21 06:53

    According to the Zen of Python:

    Explicit is better than implicit.

    ... can't argue with that, surely?

    0 讨论(0)
  • 2020-11-21 06:54

    As suggested in the docs, you should (almost) never use import * in production code.

    While importing * from a module is bad, importing * from a package is probably even worse.

    By default, from package import * imports whatever names are defined by the package's __init__.py, including any submodules of the package that were loaded by previous import statements.

    If a package’s __init__.py code defines a list named __all__, it is taken to be the list of submodule names that should be imported when from package import * is encountered.

    Now consider this example (assuming there's no __all__ defined in sound/effects/__init__.py):

    # anywhere in the code before import *
    import sound.effects.echo
    import sound.effects.surround
    
    # in your module
    from sound.effects import *
    

    The last statement will import the echo and surround modules into the current namespace (possibly overriding previous definitions) because they are defined in the sound.effects package when the import statement is executed.

    0 讨论(0)
提交回复
热议问题