Tiny mini doubts


#1



8. Generic Imports:
What is the difference between ' import math ' and ' from math import * '


Both of them import all the variables and modules in math, in what way are these different?



#2

>>> pi
Traceback (most recent call last):
  File "<pyshell#220>", line 1, in <module>
    pi
NameError: name 'pi' is not defined
>>> import math
>>> pi
Traceback (most recent call last):
  File "<pyshell#222>", line 1, in <module>
    pi
NameError: name 'pi' is not defined
>>> math.pi
3.141592653589793
>>> from math import *
>>> pi
3.141592653589793
>>>

The latter case imports the entire math module as functions, whereas the earlier case imports them as methods.

In most cases we need but a handful of math functions, so selective importing is the optimum approach.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.