简体   繁体   English

从 python class 创建 vim 实例?

[英]Create a vim instance from a python class?

I'm calling a python function that returns a dictionary, and that works but I was just wondering if there was a way to create an instance like this in vimscript m.multi(3), m.div(20,5) .我正在调用一个返回字典的 python function,它有效,但我只是想知道是否有办法在 vimscript m.multi(3), m.div(20,5)中创建这样的实例。 This is just an example, not sure about the last 2 lines.这只是一个例子,不确定最后两行。

python3 <<EOF
class Multiply:
    @staticmethod
    def myfunc(n):
        print("Answer is:", n * n)
EOF
python3 m = Multiply
python3 vim.command(f"let multi={m}")
call multi(4)

Define Multi as a VimScript function and call python inside it:Multi定义为 VimScript function 并在其中调用 python :

python3 <<EOF
import vim

class Multiply:
    @staticmethod
    def myfunc(n):
        print("Answer is:", n * n)
EOF

" Single global instance
python3 m = Multiply()

function! Multi(i)
    " int() is required as vim.eval() returns a string even for int arg
    python3 m.myfunc(int(vim.eval('a:i')))
endfunction

Test: :call Multi(4)测试: :call Multi(4)

I don't think it's possible, leave object oriented stuff in python code.我认为不可能,将面向 object 的内容留在 python 代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM