trouble testing for existance of variable

Fredrik Lundh fredrik at pythonware.com
Thu Mar 14 03:08:34 EST 2002


"googlePoster" wrote:
> I tried
>
> try:
>    ct = vim.exists("colors_name")        # doesn't work when not defined
> except vim.error:
>    curr_color = "none"
>
> if ct > 0:
>    curr_color = vim.eval('colors_name')
>
> and got
>
> AttributeError: 'module' object has no attribute 'exists'
>
> apparently 'exists' is supported in vim, but not python's
> portal to it

look at carels's example again: he's not using a vim.exists
function, he's using vim.eval to evaluate a string that con-
tains an "exists" call.

try this instead:

if vim.eval('exists("colors_name")' % name):
    curr_color = vim.eval('colors_name')

if you're going to do this a lot, it might be worth defining
a helper function:

def vim_get(name, default=None):
    if int(vim.eval('exists("%s")' % name)):
        return vim.eval(name)
    else:
        return default

curr_color = vim_get("colors_name", "none")

</F>





More information about the Python-list mailing list