Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "import *" bad?

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?

like image 825
Software Enthusiastic Avatar asked Oct 15 '22 04:10

Software Enthusiastic


People also ask

Why import * is not good?

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.

Why you shouldn't use wildcard imports?

The main drawback of using wildcard imports in Java is possible naming conflicts. Let's assume that we have developed an ArrayList class ourself which implements the java. util. List interface.

Why are wildcard imports bad Python?

import * ) When an import statement in the pattern of from MODULE import * is used it may become difficult for a Python validator to detect undefined names in the program that imported the module.

Can you use import* in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.


1 Answers

  • 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.

like image 259
gruszczy Avatar answered Oct 17 '22 18:10

gruszczy