We should not use import * in Python applications since it corrupts our namespace. Importing all functions and classes (whether required or not) into our own namespace using the import * corrupts our namespace.
This may conflict with functions you define or functions from other libraries you import It can sometimes be difficult to determine which library a certain function came from at times.
With the import * practice, there is always the possibility of overriding variables/functions, etc.
Here are some more reasons why import * should not be used:
- It is difficult to know or recognize what is imported from which module, resulting in poor code readability.
- The possibility of hidden bugs grows.
- Because pyflake-like tools cannot be utilized, we cannot discover errors in the source code statically.
- Python, as we know, allows the user to import any module required. However, in huge projects or with numerous lines of code, we will be unable to detect the user-defined function and different methods.
If you still wish to use it, you should be cautious and strive to keep it in good working order.
All of this is not to say that using import * is always a bad idea; The only thing you should remember while using import * is that you should always use it with caution and discipline.
Example:
# Import the module sampleModule using import keyword and * from sampleModule import * # Create a function say multiply() which accepts two numbers as arguments. def multiply (a, b): # Multiply both the passed numbers and return the result return a*b # Call the above created function by passing two random numbers and print the result. print (multiply (3, 2))
Explanation:
If there was a ‘multiply’ function in module “sampleModule,” the problem would be that the defined ‘multiply’ function overrides the ‘multiply’ function from module “sampleModule.” As a result, it is recommended that import * not be used in practice.
Correct Approach for the above Code:
sampleModule:
# Create a function say multiply() which accepts two numbers as arguments def multiply(a, b): # Multiply both the passed numbers and return the result print(a*b)
Approach:
- Import the module sampleModule using import keyword and *
- Create a function say multiply() which accepts two numbers as arguments.
- Multiply both the passed numbers and return the result
- Call the above-created function by passing two random numbers and print the result.
- The Exit of the Program.
Below is the Implementation:
# Import the module sampleModule using import keyword and * from sampleModule import * # Create a function say multiply() which accepts two numbers as arguments. def multiply (a, b): # Multiply both the passed numbers and return the result return a*b # Call the above created function by passing two random numbers and print the result. print (multiply (3, 2)) print(sampleModule.multiply(4,5)
Output:
6 20
Explanation:
Coding in this manner improves code readability, makes debugging easier, and virtually eliminates the possibility of conflict.(may be 0 chances of conflicts).