trouble testing for existance of variable

googlePoster scott2237 at yahoo.com
Tue Mar 12 21:09:40 EST 2002


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

the following

try:
   curr_color = vim.eval("colors_name")
except NameError:
   curr_color = "none"
   print "colors_name does not exist"
else:
   print "colorscheme: %s" % curr_color

gets the name of the colorscheme just fine when it's
defined, but when not, yields

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


grrrrrrrr


"Cliff Wells" <logiplexsoftware at earthlink.net> wrote in message
news:mailman.1015972703.8850.python-list at python.org...
> On 12 Mar 2002 13:52:01 -0800
> googlePoster wrote:
>
> > if I want to know if a variable exists before
> > testing it, what do I do?
>
> How about
>
> try:
>    colors_name
> except NameError:
>    print "it doesn't exist"
> else:
>    print "it exists"
>
> --
> Cliff Wells, Software Engineer
> Logiplex Corporation (www.logiplex.net)
> (503) 978-6726 x308  (800) 735-0555 x308
>





More information about the Python-list mailing list