Windows, IDLE, __doc_, other

Stephen Hansen apt.shansen at gmail.com
Mon Dec 21 18:19:19 EST 2009


On Mon, Dec 21, 2009 at 2:57 PM, W. eWatson <wolftracks at invalid.com> wrote:
> This has got to be some sort of IDLE issue then.

Huh? How do you figure?

> When I run a simple
> program. If I open this program in the IDLE editor:
> #import math
> print "hello, math world."
> print cos(0.5)
> print sin(0.8)
>
> then I get
>    print cos(0.5)
> NameError: name 'cos' is not defined

Of course, because -- cos is not defined. As I stated in my previous
email, "math" has to be imported to be used.

>
> OK, >>> dir()
> ['__builtins__', '__doc__', '__file__', '__name__', 'idlelib']
>
> Fine. I now change the code to include import mat get the same:
>    print cos(0.5)
> NameError: name 'cos' is not defined

Yes, because cos is inside math.

[snip
> Now, I go to the script and enter
> from math import *
> dir is now bulked up with the math functions. I change back math.cos to cos
> and the program runs well.
>
> This sort of figures. Apparently, I've added to the namespace by importing
> with *.

Apparently? -- you precisely and explicitly added every object in
'math' to your current namespace. "from math import *" does precisely
that.

> My point is that I'm betting different results. OK, fine. It appears the
> same thing happens with I modify the program itself with from math import *

Different results? What different results are you talking about?

If you want to access 'sin' without 'math.', you'll have to do 'from
math import *' in each file where you want to do that.

> So IDLE is not clearing the namespace each time I *run* the program. This is
> not good. I've been fooled. So how do I either clear the namespace before
> each Run? Do I have to open the file in the editor again each time before
> trying to Run it? I hope there's a better way.

How do you figure its 'not clearing the namespace'? In which
namespace? I fire up IDLE, and start a new file, and put in a single
line: "a = 1". I choose Run Module, and it runs it. I verify in the
interactive interpreter that a is 1. I then change that file to "a = a
+ 1", and run it. Now, it errors out-- of course-- because IDLE
"cleared" the namespace and re-ran the module.

It says in the interpreter its restarting, even.

--S



More information about the Python-list mailing list