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

M

mateus

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?
 
M

mateus

mateus said:
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?

A few specs that I forgot to mention.

Version: python-2.4.2
OS: Librix (Gentoo (Linux))
 
H

hwiechers

mateus said:
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().
 
T

Tim N. van der Leeuw

Hi Mateus,

We'd need to see more code then just this snippet. It looks like the
name 'session' is used elsewhere in the code, and is in scope for the
showReport() method.
But without seeing a bit more code of this class, and possibly global
variables / code, it's not possible to say this.

There's definately no rules in Python that allow you to 'abbreviate'
variable names.

Cheers,

--Tim
 
M

Max M

mateus said:
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?


No. You are probably running your script in an ide that keeps an old
variable hanging around.

Try it from a command promt.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
 
M

Max Erickson

mateus said:
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?

Is self.logger.items() empty?

This does not raise a NameError even when 'explode' is not defined:

for x in []:
explode


max
 
M

mateus

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

BartlebyScrivener

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.

If you are on Windows, check your PATHEXT environment variable and make
sure that .py is listed ahead of .pyc and any other .py?

See, e.g., a bug in earlier versions of ActiveState distrib

http://tinyurl.com/qok9v

rd

"Electricity is actually made up of extremely tiny particles called
electrons, that you cannot see with the naked eye unless you have been
drinking."--Dave Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top