Why is error message value "disappearing"? 
Author Message
 Why is error message value "disappearing"?

I have code that acts as the following (stripped down for purposes of
brevity)

# errlost.py ------------------------

def errtest(errMsg):
    errMsg = "no error."
    valid_call = 0
    try:
        fldname = "test"
        # do something that raises a LookupError exception.
    except LookupError, e:
        errMsg = "Warning, lookup error occurred accessing field name <%s>"
% fldname
        print "In errtest, errMsg = ", errMsg
    return valid_call

# END: errtest

# test errtest()
errMsg = ""
if not errtest(errMsg):
        print "Outside errtest, errMsg = ", errMsg

# ====================

When I run the code I get the following printout:

    In errtest, errMsg = "Warning, lookup error occurred accessing field
name <test>"
    Outside errtest, errMsg = ""

It's as if errMsg is being passed by value and the assignment that occurs
inside errtest() doesn't "stick".  I thought python was pass by reference?

What am I doing wrong?

thx

--
Robert Oschler,
Android Technologies, Inc.
http://www.*-*-*.com/
- The home of Off-Book! (tm)
The scene memorization tool for Actors and Actresses!



Sat, 26 Nov 2005 06:23:35 GMT  
 Why is error message value "disappearing"?
[...]
Quote:
> It's as if errMsg is being passed by value and the assignment that occurs
> inside errtest() doesn't "stick".  I thought Python was pass by reference?

> What am I doing wrong?

[...]

http://www.python.org/doc/FAQ.html#4.35
http://www.python.org/doc/FAQ.html#4.89

John



Sat, 26 Nov 2005 06:50:06 GMT  
 Why is error message value "disappearing"?

Quote:

> It's as if errMsg is being passed by value and the assignment that
> occurs
> inside errtest() doesn't "stick".  I thought Python was pass by
> reference?

> What am I doing wrong?

It's because there are two different errMsg variables that you're using;
one is local (to the function errtest), the other is global.  The first
is local because it's one of the formal parameters, so Python figures
you mustn't be talking about the global version.  If you really want it
to be global, don't use it:

        errMsg = ""

        def errtest():
            global errMsg # use the global variable
            ...

        if not errtest():
            ...

--

 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Courage is the fear of being thought a coward.
\__/  Horace Smith



Sat, 26 Nov 2005 07:15:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. file drop value disappears on "change"

2. Error Message "ert0082"

3. hugs "Program error" messages

4. "Too many continuation lines" error message

5. error message while do "package require"

6. The strange error message by "after"

7. Error message: can't read "tkPriv(x)": no such element in array

8. Why is this a "Parse Error"?

9. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

10. "bound error" error# 1132

11. Error with eruby: "Exec format error"

12. Error: no value given for parameter "()" to "Run"

 

 
Powered by phpBB® Forum Software