Python : Importing modules

This one is a small tip for computer programming in Python.
In python 3, I was not able to figure out what is the difference between the commands, 'import math' and 'from math import *'. Here is what I discovered about that.

When you import a module by the command import, eg: - 'import math', then what happens is , though it imports the module we must specify the module name first to access the functions inside it. Eg: - math.sqrt()

If we do 'from math import *', what happens is all the functions in the math module gets loaded in the main workspace. That is when you use the '*'. We can specifically type the name of the functions just required. The functions can be accessed by just their name. Eg: - sqrt() without the name of the module. 

There are pros and cons for these methods. In the second method, if the variables or functions defined in the main, matches with the name of the function inside the importing module, it may cause unexpected behaviour. At some cases this is called contaminating the namespace. That is one cons. Because you must be clear that you want to perform the method square root (sqrt) inside the module 'math'
Do you agree?
Stay tuned for more.

Comments