Windows, IDLE, __doc_, other

W. eWatson wolftracks at invalid.com
Mon Dec 21 17:57:48 EST 2009


Stephen Hansen wrote:
> On Mon, Dec 21, 2009 at 1:51 PM, W. eWatson <wolftracks at invalid.com> wrote:
>> Lie Ryan wrote:
>>> On 12/22/2009 6:39 AM, W. eWatson wrote:
>>>> Wow, did I get a bad result. I hit Ctrl-P, I think instead of Alt-P, and

> 
> No, its not true. A built-in module does not mean its available
> everywhere. It means its built into Python itself-- e.g., its directly
> linked into the python dll and not a separate file (like most of the
> the standard library).
> 
> Lots of modules are built-in, but they don't pollute the __builtins__
> / universal namespace. You import them to access them. If you want you
> can "from math import *" if you want the math module to fill out your
> module namespace (I don't recommend *'s personally, its better to
> import symbols explicitly by name)
> 
> --S
This has got to be some sort of IDLE issue then. 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

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

Now, >>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math']
 >>>
math is now available. so I change cos(0.5) to math.cos(0.5) and get
     print sin(0.8)
NameError: name 'sin' is not defined
Fine, it needs math.
dir() is the same.

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 *.

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 *

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.




More information about the Python-list mailing list