Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. Also it becomes very difficult at some times to say from which library does a particular function came from. The risk of overriding the variables/functions etc always persist with the import * practice.
Below are some points about why import * should not be used:
- Code Readability
- It is always remains a mystery what is imported and cannot be found easily from which module a certain thing was imported that result in low code readability.
- Polluting the namespace, import * imports all the functions and classes in your own namespace that may clash with the function and classes you define or function and classes of other libraries that you may import.
- Concrete possibility of hiding bugs
- Tools like pyflakes can’t be used to statically detect errors in the source code.
All of this does not mean that using import * is always bad, if i had told you that there is nothing like import * thing on this universe you would have been craving for it. The only thing you should remember while using import * is that you should always use this carefully and with discipline maintained.
Now lets dive into an example to see the problem in a more practical and easy to understand way.
consider a package a that contains a function sum (a, b)
the error with this code is that the sum function that we define overrides the sum function from the module ‘a’ that we imported and we don’t even have any idea about it. also it becomes very difficult to identify which function is actually being called in case of large programs.
Coding this way increases code readability as well as it becomes easy to debug and there are almost zero chances that any conflict will occur.
- Import module in Python
- Create and Import modules in Python
- ES6 | Import and Export
- What is the best way to include CSS file? Why use @import?
- import command in Linux with Examples
- Different ways to import csv file in Pandas
- What is the use of star-preceded property in CSS ?
- Print the Alphabets A to Z in Star Pattern
- Project Idea | (A Game of Anagrams )
- Simplified International Data Encryption Algorithm (IDEA)
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ShivamShahi