[Numpy-discussion] cos -- NameError: global name 'cos' is not defined
Wayne Watson
sierra_mtnview at sbcglobal.net
Mon Dec 21 18:11:41 EST 2009
Yes, thanks. That's the what I finally changed to. This originated up a
thread or so when I displayed the highly populated code with math. Some
said I didn't need it, so I thought I'd give it a go.
I just started plugging away again with IDLE and am pretty convinced
that IDLE is something of an enemy. I started afresh loading this into
the editor:
import math
print "hello, math world."
print math.cos(0.5)
print math.sin(0.8)
Run works fine. No errors. Now I do:
>>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math']
>>>
OK, swell. Now I import via the script window
>>> import numpy as np
>>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math', 'np']
I think I'm adding to the namespace both the program the script sees.,
because adding this ref to np in the program works fine.
import math
print "hello, math world."
print math.cos(0.5)
print math.sin(0.8)
print np.sin(2.2)
I've been assuming that IDLE clears the namespace. It's quite possible
that I get anomalous results as I move between Run the program via the
editor, and fiddling in script land. I would like to think that IDLE has
some way to clear the namespace before it runs the program. If not, yikes!
Robert Kern wrote:
> On Mon, Dec 21, 2009 at 11:40, Wayne Watson
> <sierra_mtnview at sbcglobal.net> wrote:
>
>> In this code,
>> ===========start
>> import math
>> import numpy as np
>> from numpy import matrix
>> def sinD(D): # given in degrees, convert to radians
>> return sin(radians(D))
>> def cosD(D):
>> return cos(radians(D)) <<--------------
>> def acosD(D):
>> acos(radians(D))
>> return=====end
>> the << line produces, "NameError: global name 'cos' is not defined", but
>> the sin() above it does not? They are both built-in functions.
>>
>
> No, they aren't. They are in the math module. You want math.cos(). The
> same goes for radians() and acos() and sin().
>
>
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"... humans'innate skills with numbers isn't much
better than that of rats and dolphins."
-- Stanislas Dehaene, neurosurgeon
Web Page: <www.speckledwithstars.net/>
More information about the NumPy-Discussion
mailing list