trouble testing for existance of variable

Peter Hansen peter at engcorp.com
Tue Mar 12 21:32:18 EST 2002


googlePoster wrote:
> 
> in the interest of full disclosure, I should have said
> since I am using python as a gvim scripting tool I
> must import the gvim variable, colors_name, before
> I can work with it

Ah, posting real code is always the best approach. :)

> E121: Undefined variable: colors_name
> E15: Invalid expression: colors_name
> Traceback (most recent call last):
>   File "<string>", line 1, in ?
>   File "testMe.py", line 36, in ?
>     curr_color = vim.eval("colors_name")
> vim.error: invalid expression

"vim.error" is the key.  It is highly likely that that is
a vim-specific exception which you should be catching instead
of NameError.  Try this:

try:
    curr_color = vim.eval("colors_name")
except vim.error:
    curr_color = None

if curr_color:
    print "colorscheme: %s" % curr_color
else:
    print "no color scheme"

You might also try typing 

:py import vim; vim.error.__class__.__bases__

and see what it reports.  If it mentions something
like (<class exceptions.ValueError at 0x09d98d987d>,)
you will have a better idea what it represents.
RTFM might help too, if you can find a mention of it.

-Peter



More information about the Python-list mailing list