[英]Python, VIM: namespace, scope, life of a python object stuck in a << HERE
If i have two functions: 如果我有两个功能:
function! foo()
python3 << HERE
import mylib
pass
HERE
function! bar()
python3 << HERE
import mylib
pass
HERE
The src says: src说:
1. Python interpreter main program
3. Implementation of the Vim module for Python
So, is the python interpreter embedded in vim AND additionally, are separate extensions to python provided (wrapper functions for the VIM API) 因此,是否在vim中嵌入了python解释器,此外,是否提供了对python的单独扩展(VIM API的包装函数)
How many times is mylib compiled to bytecode and loaded? mylib被编译为字节码并加载多少次? Does each vimscript function get its own mylib - can I instantiate something and expect it to be visible in the other function? 每个vimscript函数是否都有自己的mylib-我可以实例化某些东西并期望它在其他函数中可见吗? I have a bunch of leader functions that share similar code and act on the same buffer so I wanted to know if I could reuse that data-structure. 我有一堆领导函数,它们共享相似的代码并作用于同一缓冲区,因此我想知道是否可以重用该数据结构。 How many times is the interpreter loaded into memory: once obviously at vim runtime. 解释器加载到内存中的次数:显然是在vim运行时。
sys.modules
, so mylib is loaded once per Vim instance. Python解释器将导入的模块缓存在sys.modules
,因此每个Vim实例都将mylib加载一次。 *.pyc
files on the filesystem, so mylib is compiled once per its update (assuming Python was able to detect this update; usually it has no problems with this). Python解释器将已编译的字节码缓存在文件系统的*.pyc
文件中,因此mylib每次更新都会被编译一次(假设Python能够检测到此更新;通常对此没有问题)。 In any case, do never use python << EOF
, all plugins should always use namespaced functions: eg 无论如何,切勿使用python << EOF
,所有插件都应始终使用命名空间函数:例如
Bad: 坏:
python << EOF
from y import foo, bar
def my_function1():
foo()
def my_function2(i):
bar(i)
for i in range(2):
my_function1()
my_function2(i)
EOF
Good: 好:
# pythonx/mymodule.py
from y import foo, bar
def my_function1():
foo()
def my_function2(i):
bar(i)
def start():
for i in range(2):
my_function1()
my_function2(i)
" plugin/myscript.vim
python import mymodule; mymodule.start()
Reasoning: 推理:
my_function1
, my_function2
, foo
, bar
, i
appear in the __main__
module global namespace. 在上述例子中所有的my_function1
, my_function2
, foo
, bar
, i
显示在__main__
模块全局命名空间。 If you install some plugin which does not follow good practices (there are lots of them) and it appears to define different foo
or bar
your plugin will not work. 如果您安装了一些不遵循良好做法的插件(其中有很多),并且似乎定义了不同的foo
或bar
您的插件将无法工作。 Or that plugin will not work if it happened to be loaded before yours. 否则,如果该插件恰好在您的插件之前被加载,则该插件将无法工作。 python << EOF
is recompiled each time it is called. python << EOF
每次被重新编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.