Variable name has a typo, but code still works. Why?

mateus mateus.justino at gmail.com
Thu Jun 1 01:09:32 CEST 2006


Well, one by one I checked for the presence of both sessions and
session in the globals dictionary (within showReport(), but outside of
the loops).

Neither one of them existed previously, thus and I received the
exception about them not being found:


  File "/home/mjpl/hct/repository/hct/tutoo.py", line 219, in loadNext
    self.loadStage(self.cur+1)
  File "/home/mjpl/hct/repository/hct/tutoo.py", line 195, in loadStage
    self.stageFrame.show()
  File
"/home/mjpl/hct/repository/hct/stages/resultadoframe/resultadoframe.py",
line 17, in show
    self.listView.showReport()
  File "/home/mjpl/hct/repository/hct/widgets/deviceview.py", line 54,
in showReport
    print globals()['session']
KeyError: 'session'

So, I tried using other variable names for the outer and inner loops
with the only difference of one letter.  I then got the expected
message about the variable name not being encountered.

I returned the variable names to 'sessions' and 'session' respectively,
and I got the same error about the name 'session' not being founded.  I
can only assume that there was some type of cache problem.  Could it
have been in the .pyc?  I thought that was recompiled every time a .py
is run/set to be interpreted.  I'm sure I got that last sentence wrong.

hwiechers at gmail.com wrote:
> mateus wrote:
> > print "hello world"
> >
> > I have a nested loop where the outer loop iterates over key value pairs
> > of a dictionary and the inner loop iterates over a list each list of
> > which is a mapped value from the dictionary
> >
> >     def showReport(self):
> >         for dev, sessions in self.logger.items():
> >             for tree in session:
> >                 self.addTestItem(self, tree)
> >
> > What I don't understand is why this executes w/o any problems when
> > "sessions" was spelled as plural (sessionS) while later being spelled
> > in the singular (session).
> >
> > Is there some type of name resolution of local variables where Python
> > makes assumptions?
>
> I've never heard of a rule disregarding ending 's'es and I really doubt
> one
> exists.
>
> Are you sure session isn't a global variable? You can check using
> globals().




More information about the Python-list mailing list