Windows, IDLE, __doc_, other

Lie Ryan lie.1296 at gmail.com
Tue Dec 22 13:13:41 EST 2009


On 12/22/2009 12:06 PM, W. eWatson wrote:
> Stephen Hansen wrote:
>> On Mon, Dec 21, 2009 at 2:57 PM, W. eWatson <wolftracks at invalid.com>
>> wrote:
>> [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.

After a script's execution, IDLE's shell namespace uses the last 
scripts's namespace; this is similar to using the -i switch in the terminal:
$ python -i myscript.py
program output
 >>> foo() # foo() is a function defined in myscript.py

this is often useful for debugging

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

You must be starting IDLE without subprocess. Did you see this message

IDLE 2.6.1      ==== No Subprocess ====

when starting IDLE.

If you're on Windows, don't use the "Edit with IDLE" right-click hotkey 
since that starts IDLE without subprocess. Use the shortcut installed in 
your Start menu.

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

That is irrelevant with numpy. If you start IDLE with subprocess, then 
every time before you run a script this message appears:

 >>> ============================= RESTART =============================

PS: you can force IDLE to restart the subprocess with Ctrl+F6

>> It says in the interpreter its restarting, even.

When IDLE is not run with subprocess, running a script is equivalent to 
copy and pasteing the script to the shell.



More information about the Python-list mailing list