Python Math libraries - How to?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri May 9 08:16:08 CEST 2008


En Thu, 08 May 2008 13:17:51 -0300, delta2 <marc.messina at sbcglobal.net>  
escribió:

> I am also using Zelle's book to teach myself programming and Python.  I  
> also
> had a problem with " import math ", but the alternative of " from math
> import * "  is working for me.  I don't know why one works and the other
> doesn't.

Note that they do different things. When you execute `import math`, the  
math module is loaded, but the ONLY new name you get is `math`. If you  
want the sqrt function, you have to use math.sqrt(2)
When you execute `from math import *`, the math module is loaded the same  
as above, but ALL the public names defined in that module are now  
available in the current namespace. So the sqrt function is now available  
simply as sqrt

Note that the form `from xxx import *` is intended mainly for usage in the  
interactive interpreter, for testing and playing only. If you use that  
form from a few modules, the namespace becomes too cluttered, then you  
don't know from where some name is coming, and a later import may  
inadvertidely replace previous names.

-- 
Gabriel Genellina




More information about the Python-list mailing list