[Tutor] Help with pi and the math module.

paul brian paul1brian at gmail.com
Mon Sep 26 11:29:07 CEST 2005


it might be a little clearer if you look at sys.modules


In the sys module is a useful dictionary called "modules"
This dictionary maps all the names of modules we import, to the objects
that are those modules.

for example (I am only importing pprint to make it easier to read)

>>> import sys, pprint
>>> pprint.pprint( sys.modules )
{'UserDict': <module 'UserDict' from 'C:\Python24\lib\UserDict.pyc'>,
 '__builtin__': <module '__builtin__' (built-in)>,
 '__main__': <module '__main__' (built-in)>,
 'copy_reg': <module 'copy_reg' from 'C:\Python24\lib\copy_reg.pyc'>,
 ...
 'sys': <module 'sys' (built-in)>,
 'types': <module 'types' from 'C:\Python24\lib\types.pyc'>,
 'warnings': <module 'warnings' from 'C:\Python24\lib\warnings.pyc'>,
 'zipimport': <module 'zipimport' (built-in)>}


So how does an import change this "namespace"?

Well, lets try one. I am going to choose the math module, becasue it has an
easy to spot constant in there called pi.

>>> import math
>>> pprint.pprint( sys.modules )
...
 'math': <module 'math' (built-in)>,
...

aha - that was not there earlier.

>>> import sys, pprint
>>> import math
>>> sys.modules['__main__'].__dict__['pi']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'pi'
>>> sys.modules['__main__'].__dict__['math']
<module 'math' (built-in)>
>>> math.pi
3.1415926535897931


So in the namespace of __main__ (where we run the interpreter) there
exists a module named 'math', and this module holds in its namespace a constant
called pi.

Now lets restart our python interepreter and try again

>>> import sys, pprint
>>> from math import *
>>> import sys, pprint
>>> pprint.pprint( sys.modules )
...
 'math': <module 'math' (built-in)>
...

There it is again. ?

Now if i have a look at the __main__ namespace (its __dict__)

>>> sys.modules['__main__'].__dict__['math']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'math'
>>> sys.modules['__main__'].__dict__['pi']
3.1415926535897931

math is not in the namespace, but pi is directly there.
Thats the difference between

import math which imports a module into the current namespace

and

from math import *
 which imports all the contents of math into the namespace

one last thing

>>> import random as offthewall
>>> pprint.pprint( sys.modules )
...
 'pprint': <module 'pprint' from 'C:\Python24\lib\pprint.pyc'>,
 'random': <module 'random' from 'C:\Python24\lib\random.pyc'>,
...
>>> random.randint(1,10)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'random' is not defined

Whoops - but it is clearly shown in the sys.modules. That is correct - because
we have imported the module (file) called random.  However when I am in
the __main__ namespace and do random.randint(1,10), Python tries to find
"random" in the __main__namespace.

>>> sys.modules['__main__'].__dict__['offthewall']
<module 'random' from 'C:\Python24\lib\random.pyc'>
>>> sys.modules['__main__'].__dict__['random']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'random'


We have imported the module random but with a name of offthewall
so

>>> offthewall.randint(1,10)
1
>>> offthewall.randint(1,10)
8

works fine.



On 9/24/05, Pujo Aji <ajikoe at gmail.com> wrote:
> hi,
>
> if you use : import math
> you can type: diameter * math.pi
>
> if you use from math import *
> you can type: diameter * pi
>
> Cheers,
> pujo
>
>
> On 9/24/05, Nathan Pinno <falcon3166 at hotmail.com> wrote:
> >
> >
> > Hi all,
> >
> > I need help with pi and the math module. I had import math at the top of a
> program, but when it came to diameter*pi, it said that pi was not defined.
> >
> > How do I use it correctly?
> >
> > Thanks,
> > Nathan Pinno
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--------------------------
Paul Brian
m. 07875 074 534
t. 0208 352 1741


More information about the Tutor mailing list