Why is error message value "disappearing"?

Erik Max Francis max at alcyone.com
Mon Jun 9 19:15:29 EDT 2003


Robert Oschler wrote:

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

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Courage is the fear of being thought a coward.
\__/  Horace Smith




More information about the Python-list mailing list