cos -- NameError: global name 'cos' is not defined
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. -- 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/>
On Mon, Dec 21, 2009 at 11:40, Wayne Watson <sierra_mtnview@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(). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
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@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/>
On Mon, Dec 21, 2009 at 6:11 PM, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
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!
idle has two modes depending on whether it is started with or without -n option on the command line. I usually pick this option depending on what I am doing. From the IDLE help: Running without a subprocess: If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible. Josef
Robert Kern wrote:
On Mon, Dec 21, 2009 at 11:40, Wayne Watson <sierra_mtnview@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/>
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
josef.pktd@gmail.com wrote:
On Mon, Dec 21, 2009 at 6:11 PM, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
...
) 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!
idle has two modes depending on whether it is started with or without -n option on the command line. I usually pick this option depending on what I am doing. From the IDLE help:
Running without a subprocess:
If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible.
Josef
I'm running under Win XP. If there are command line options, I'm not aware of them. I tried the reload in the script window, but got nowhere with it. Is it usable in the program itself? Ah, I'm looking in my copy of Core Python by Chun, and gives some details on it. The IDLE Help for itself does not mention reload(). Maybe I need to a Win command line get away from IDLE. I started into iPython, but slipped back to IDLE. I think it may need another chance.
I may have inadvertently made a slip between using script versus shell. What I'm getting at it that the namespace is the same for both the editor window and shell window. I find that a little bizarre. I would have expected each Run from the editor to clear all modules, and only load those shown in the editor.
On Mon, Dec 21, 2009 at 8:25 PM, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
I may have inadvertently made a slip between using script versus shell. What I'm getting at it that the namespace is the same for both the editor window and shell window. I find that a little bizarre. I would have expected each Run from the editor to clear all modules, and only load those shown in the editor. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
I'm not really sure what you mean with "What I'm getting at it that the namespace is the same for both the editor window and shell window" I'm also on WindowsXP, I'm using the subprocess (-n) option of IDLE in the following way: starting on the command line with -n as in
"C:\Programs\Python25\pythonw.exe" "C:\Programs\Python25\Lib\idlelib\idle.pyw" -n
which I think is the default in the programs shortcut, only uses one process for interpreter shell and idle and reload is not possible. When I start IDLE with
"C:\Programs\Python25\pythonw.exe" "C:\Programs\Python25\Lib\idlelib\idle.pyw"
then the interpreter is run in a separate process. Whenever I hit F5 then it restarts the process with just the script in the editor. Idle in this case also has an option to "Restart Shell" under the Shell menu. In this case there are no spill-overs from one run to the next, and you only get what is in the script. To make my life easier, I associated two (actually I have 4 - same for python 2.4 and 2.5) different ways of starting a .py file when I right click a file in windows explorer. I changed/added the file association in file types in the tools-folder options menu of windows explorer, corresponding to the two options "C:\Programs\Python25\pythonw.exe" "C:\Programs\Python25\Lib\idlelib\idle.pyw" -n -e "%1" "C:\Programs\Python25\pythonw.exe" "C:\Programs\Python25\Lib\idlelib\idle.pyw" -e "%1" when I'm just experimenting with a script, I choose option with -n to avoid long import times. When I need to make sure that all dependencies/imports are reloaded, I start without -n. However, it is only possible to have one interpreter with -n open at one time (and right now I have 9 separate python idle running, one of them without -n) But actually, I'm using Spyder now most of the time, which let's you choose at runtime, whether to run a script in a separate process (external shell), in the same process as spyder and the interactive interpreter (internal shell), or to execute just a few selected lines (as F9 in matlab, I think) in the interpreter (also in internal shell). Except for a few editor quirks, Spyder works very well. Note: all editors when they run shell and editor in the same process have some background noise (or magic). But with separate subprocesses or external shells that allow restart, you loose some of the interactivity. Josef
On Mon, Dec 21, 2009 at 9:40 AM, Wayne Watson <sierra_mtnview@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.
sin(10) NameError: name 'sin' is not defined
Oh, right, there is no built-in sin function. I need to import it:
import numpy as np import math
math.sin(1) 0.8414709848078965 np.sin(1) 0.8414709848078965
or
from numpy import sin sin(1) 0.8414709848078965
Yes, one can get both sin and cos via the interactive shell, if math is imported as you have done. However, I thought math itself always present to a program module? In my program, sin exists but not cos, so one is forced to use math.cos(). Why one but not the other? Keith Goodman wrote:
On Mon, Dec 21, 2009 at 9:40 AM, Wayne Watson <sierra_mtnview@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.
sin(10)
NameError: name 'sin' is not defined
Oh, right, there is no built-in sin function. I need to import it:
import numpy as np import math
math.sin(1)
0.8414709848078965
np.sin(1)
0.8414709848078965
or
from numpy import sin sin(1)
0.8414709848078965
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- 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/>
On Mon, Dec 21, 2009 at 13:44, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
Yes, one can get both sin and cos via the interactive shell, if math is imported as you have done. However, I thought math itself always present to a program module?
No.
In my program, sin exists but not cos, so one is forced to use math.cos(). Why one but not the other?
Presumably you have imported it somewhere. Please show your program, and we may be able to point it out to you. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Thanks, but I think I've got this under control now, and am moving on. Robert Kern wrote:
On Mon, Dec 21, 2009 at 13:44, Wayne Watson <sierra_mtnview@sbcglobal.net> wrote:
Yes, one can get both sin and cos via the interactive shell, if math is imported as you have done. However, I thought math itself always present to a program module?
No.
In my program, sin exists but not cos, so one is forced to use math.cos(). Why one but not the other?
Presumably you have imported it somewhere. Please show your program, and we may be able to point it out to you.
-- 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/>
participants (4)
-
josef.pktd@gmail.com -
Keith Goodman -
Robert Kern -
Wayne Watson