[Tutor] (no subject)
Steven D'Aprano
steve at pearwood.info
Fri Oct 29 04:55:58 CEST 2010
Luke Paireepinart wrote:
> On Thu, Oct 28, 2010 at 8:11 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>>> postPos=words[3]
>> This like will fail with NameError, because words is not defined anywhere.
>>
>> This is not the code you are running. We can't tell what is wrong with the
>> code you run when you show us something completely different.
>>
>> Please send us the *actual* code you run, not something you've re-typed from
>> memory, or copied and pasted in bits and pieces from different places.
>>
> Actually Steven, if you are using IDLE (as a lot of new programmers
> are), the code by default does NOT execute in a subprocess but instead
> in the same interpreter IDLE is running. Therefore it can actually
> have variables stick around from previous executions.
And? That's no different from running code in the default Python
interactive interpreter.
If you have run code that has an effect on the results you are getting,
and don't show that code, then the code you have run is not the code you
have shown. Just like I said :)
> So if he didn't
> have that line in his code the first time he executed, then words
> would be defined. Then the second time perhaps he added that line,
> and in that case it will actually succeed because an old value of
> 'words' is floating around.
Exactly. The code the Original Poster *actually* executed included a
line that defined words. In this case, any extra code is probably
innocuous, but next time, who knows? I then went on to identify the
likely cause of the problem -- the OP fails to redefine postPos each
time through the loop. But can I be sure? No, because I don't know what
else the OP did. I can't think of anything realistic that might have an
effect, but perhaps that's my failure of imagination.
When you have a problem with code, it is *vital* that you know what code
is actually being executed. There are very few programming tasks harder
than trying to debug code that doesn't actually contain any bugs, or
contains bugs different from the ones you are seeing, because the code
you are actually executing is something different from what you think
you are executing.
You know the joke about the drunk who lost his keys in the park, and
then spends the next few hours searching under the light pole in the
street because the light is better there? That's what it can be like.
> It also causes issues because if you import a module from the
> interpreter and then use it in your code, it will work only during
> that instance, but if you try to run the code standalone it will fail
> when you try to use the module.
>
> I know that sounds weird and that's why IDLE should be configured to
> run as a subprocess.
Regardless of whether you are using IDLE, some other IDE, or just the
Python interpreter, you should never post code until you are sure that
it is the actual code that is being executed. If that means quitting
IDLE and starting it again in a fresh session, then do so.
For anything more than six or eight lines of code, I never trust I
understand it until I've seen it run in a fresh environment.
--
Steven
More information about the Tutor
mailing list