does this exception mean something else?

MRAB python at mrabarnett.plus.com
Thu May 6 21:03:59 EDT 2010


Alex Hall wrote:
> Hi all,
> I have a file, pasted below for what good it will do, which makes a
> couple conditional calls to a function called writeDefaults. However,
> when I manually trigger a condition that causes the function to be
> called, Python gives me a name error and says that my writeDefaults
> does not exist. I am sure that it does, as I copied the text from the
> call and pasted it into the text search of my editor, and it found the
> line that defines the function, so the text in the call is obviously
> correct. Sometimes, especially in a language like Javascript, one
> error can mean something else or point to a problem other than the one
> it claims to be about, so I wondered if the same thing could be
> happening here? The error is on line 55, just after the if statement
> that uses os.path.exists to ensure my ini file is really there. TIA!
> BTW, speech is just a file that will output a given message through an
> active screen reader; changing all calls to print will work just as
> well.
> 
You need to remember that 'def' and 'class' are _statements_, not
declarations; they have the side-effect of adding a name to the current
namespace.

A Python script is run from the first line. If/when Python reaches line
55 the 'def' statement that defines the function 'writeDefaults' hasn't
been run yet.

BTW, you should avoid 'bare excepts' (an except clause that doesn't say
which exception to catch) because they catch _every_ exception, even
KeyboardInterrupt and NameError.

You might also want to look at PEP 8:

     http://www.python.org/dev/peps/pep-0008/

which is the Style Guide for Python code.



More information about the Python-list mailing list