Windows, IDLE, __doc_, other

W. eWatson wolftracks at invalid.com
Mon Dec 21 20:06:39 EST 2009


Stephen Hansen wrote:
> 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.
Yes, I agree.
> 
>> 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.
True enough. Hang in there.
> 
> [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.
Well, it's a big surprise to me, because I thought each time I ran from 
the editor that it reloaded the modules in my imports, and cleared out 
any it didn't find.
> 
>> 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?
It seems to me as I fool around with interpreter under the script 
window, I"m creating a mess out of the namespace the program uses, and 
the program responds incorrectly.
> 
> 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
Try this sequence.
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 shell 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)      <<<<-------

There's no np in that code, but yet it works. It must be in the 
namespace it sees, and it was put there through the interactive shell.

> 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.
Hmmm, that appears to contrary to my numpy experience. I've never seen 
any re-starting msg.
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit 
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
> 
> It says in the interpreter its restarting, even.
> 
> --S



More information about the Python-list mailing list