473,594 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.ite ms():
for tree in session:
self.addTestIte m(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?

May 31 '06 #1
7 1321

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.ite ms():
for tree in session:
self.addTestIte m(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))

May 31 '06 #2

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.ite ms():
for tree in session:
self.addTestIte m(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().

May 31 '06 #3
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

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.ite ms():
for tree in session:
self.addTestIte m(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?


May 31 '06 #4
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.ite ms():
for tree in session:
self.addTestIte m(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
May 31 '06 #5
"mateus" <ma************ @gmail.com> 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.ite ms():
for tree in session:
self.addTestIte m(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.ite ms() empty?

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

for x in []:
explode
max

May 31 '06 #6
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.s howReport()
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.

hw*******@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.ite ms():
for tree in session:
self.addTestIte m(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().


May 31 '06 #7
> 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

"Electricit y 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

Jun 1 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
6459
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
11
3336
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option Explicit and Response.Expires=-1,
6
2380
by: Al Jones | last post by:
This is a repost form the vbscript newgroup - if this isn't the appropriate group would you point me toward one that is. Basically, I seem to be losing session data part way though preparing an email from (possibly) three seperate forms. the following code is the end of a routine which stashes data from the first form off to session variables and then redirects itself to the proper form / procedure depending upon the state of two...
134
7833
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
5
1950
by: Colleyville Alan | last post by:
I have built a SQL statement that is trying to loop through the fields of a table that was built from a spreadsheet and hence is "short and fat". So rather than hard-coding, I have a loop from field #6 to the last field (#63). When I use the fld variable (type integer) in the loop, I have no problem with this line which is near the end of the SQL statement: strMySql = strMySql & "Accts., Accts.Manager, " & "," But when I try to...
4
1578
by: archmonkey | last post by:
Hello Is there a way to get a pointer to a variable through the variable name represented as a string? Let me explain it through an example: /* Start code example */ int MyVar = 3;
17
1677
by: GinTon | last post by:
How to access to a variable (that value is not returned) from a module imported? And the variable is set at the module-level. That module is external to my program, it's from another project so I wann't modifying it.
12
1474
by: Gandalf | last post by:
I don't understand why this expression doesn't work: $var= '$_POST'; $name= ${$var}; the ${$var} expression wont return the same expression of $_POST. why does it happens, and how can i solve it?
3
7094
by: tshad | last post by:
Using VS 2005: Here is a piece of code that is saying that a variable doesn't exist which obviously does: public string UpdateTables(TableFieldList tableFields, UnmappedNamesCollection unmappedNames, DataView dv) { bool newSection = false; bool parameterExists = false; <---- Here is defined
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8003
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6654
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.