如何获取从 Python 中的函数返回的值 &脚本? [英] How do I get the value returned from a function in Python & Vimscript?

查看:32
本文介绍了如何获取从 Python 中的函数返回的值 &脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 编写一个 vim 插件,但是在处理 Vimscript 时出了点问题.

I'm using Python to write a vim plugin, but there's something wrong when dealing with Vimscript.

function! Login()  
python << EOF    
import vim, weibo
appkey = 'xxx'
appsecret = 'xxxxx'
callback_url = 'xxxxxxxx'
acs_token = 'xxxxx'
expr_in = 'xxxx'
client = weibo.APIClient(app_key=appkey, app_secret=appsecret,\
         redirect_uri=callback_url)
client.set_access_token(acs_token, expr_in)
del vim.current.buffer[:]  
EOF    
return client
endfunction


function! Post()  
python << EOF
import vim, weibo
try: 
    vim.command("let client = Login()")  # line1
    client.post.statuses__update(status="hello") # line2
except Exception, e:
    print e
EOF
endfunction

这里,当我调用 Post() 时,总是出现未定义变量"和无效表达式"之类的错误,但第 2 行总是成功执行.

Here, there're always errors like "Undefined variable" and "Invalid expression" when I call Post(), but line2 always succeeds executing.

我以前没有学过 Vimscript,谁能告诉我应该怎么做才能避免这些错误?

I've not learned Vimscript before, could anybody tell me what I should do to avoid those errors?

推荐答案

既然您现在已经添加了整个函数...

Since you have now added your whole function...

您在Login() 中得到未定义变量和无效表达式的原因是因为客户端的范围以EOF 结束.在返回行 vim 不知道 client 因为它只在 python 块中定义.

The reason you get an undefined variable and Invalid expression in Login() is because the scope of client ends at EOF. At the return line vim does not know about client because it is only defined within the python block.

您可以做的只是在 Post() 中定义一个为您执行此操作的 Python 函数.类似于以下内容.

What you could do is just define a python function that does this for you inside Post(). Something like the following.

python << EOF
import vim, weibo
def Login():
    appkey = 'xxx'
    appsecret = 'xxxxx'
    callback_url = 'xxxxxxxx'
    acs_token = 'xxxxx'
    expr_in = 'xxxx'
    client = weibo.APIClient(app_key=appkey, app_secret=appsecret,\
             redirect_uri=callback_url)
    client.set_access_token(acs_token, expr_in)
    del vim.current.buffer[:]  
    return client
EOF

function! Post()  
python << EOF
try: 
    client = Login()
    client.post.statuses__update(status="hello")
except Exception, e:
    print e
EOF
endfunction

注意:由于所有内容都传递给同一个 python 实例,因此您可以将 Login() 定义为 vim 函数之外的普通 python 函数,然后做任何你想做的事.它不需要在同一个 python 块中.

NOTE: Since everything is passed to the same instance of python you can just define Login() as a normal python function outside of the vim function and do what ever you want later. It does not need to be in the same python block.

旧答案

您需要将符号 EOF 放在 Python 部分的末尾.否则 vim 会继续向 python 提供命令.

You need to put the symbol EOF at the end of your python sections. Otherwise vim keeps feeding the commands to python.

python << EOF

vim 帮助中的相应部分 :h python-commands 复制到下面.

The appropriate section from the vim help :h python-commands is copied below.

:[range]py[thon] << {endmarker}
{script}
{endmarker}
            Execute Python script {script}.

{endmarker} must NOT be preceded by any white space.  If {endmarker} is
omitted from after the "<<", a dot '.' must be used after {script}, like
for the |:append| and |:insert| commands.
This form of the |:python| command is mainly useful for including python code
in Vim scripts.

您的 {endmarker}EOF.但是,由于您没有显示整个功能,我不确定您需要将 EOF

Your {endmarker} is EOF. However since you do not show the whole function I am not sure where you need to put EOF

至于你的代码.

vim.command("let obj = Login()")

这一行是正确的.当且仅当 Login() 执行没有错误.但是,您显示的代码片段登录有错误.

This line is correct. If and only if Login() executes without error. However with the snippet you show Login has errors.

这篇关于如何获取从 Python 中的函数返回的值 &amp;脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆