2

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.

m0bi5
  • 8,900
  • 7
  • 33
  • 44

4 Answers4

0

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']
>>> 
Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
0

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 -

  1. If the module defines a __all__ list, it imports the names from within that list.

  2. 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
Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

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.

Henry Zhu
  • 2,488
  • 9
  • 43
  • 87
0

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

    • Cons: Typing module.foo in your code can be tedious and redundant (tedium can be minimized by using import module as mo then typing mo.foo).

    from module import foo

    • Pros: Less typing to use fooMore control over which items of a module can be accessed
    • Cons: To use a new item from the module you have to update your import statement, You lose context about foo. For example, it's less clear what ceil() does compared to math.ceil()

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.

ifixthat
  • 6,137
  • 5
  • 25
  • 42