What is the difference between
import math
and
from math import *
I am new to python and learning the language from codeacademy.Can anyone elaborate the difference . Thanks.
What is the difference between
import math
and
from math import *
I am new to python and learning the language from codeacademy.Can anyone elaborate the difference . Thanks.
You should open up the interpreter and play:
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'math']
>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'math', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>
When you do import math
, it imports the module math
, then you can use the functions, variables and classes directly inside it using math.<name>
.
When you do from math import *
, it imports the names from within the module , there are two ways for this -
If the module defines a __all__
list, it imports the names from within that list.
Else, it imports all other variables,functions, classes , but not submodules (if any).
But please note in the second case, it does not import the math
module, instead it imports the names from within it. So instead of using math.<name>
, you would just use <name>
.
Please note, it is usually considered bad to do from <module> import *
. You can check here for reasons - Why is "import *" bad? .
Sample behavior -
>>> import math
>>> math.ceil(1.2)
2
>>> from math import *
>>> ceil(1.2)
2
in import math
, you have to state the library
before you enter the method
.
Like this:
math.floor(5.3)
But with from import math *
, you are using methods directly from that library, so all you have to type is the following:
floor(5.3)
The second method saves typing, although you can run into some problems if you accidentally name a method
that coincides with one of the methods from that library
. The first library is also more specific and clear regarding what the methods do. Also with the first method, you don't need to add some extra imports
of items that you would need if you used the second method.
Generally I prefer the first method.
The difference between import module and from module import foo is mainly subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide.
import module
Pros: Less maintenance of your import statements. Don't need to add any additional imports to start using another item from the module
from module import foo
Either method is acceptable, but don't use from module import *
For any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy to get to the point where you think you don't use the import any more but it's extremely difficult to be sure.