Python, VIM: namespace, scope, life of a python object stuck in a << HERE

Peter Otten __peter__ at web.de
Wed Nov 5 02:44:13 EST 2014


Veek M wrote:

> If i have two functions:
> 
> function! foo()
> python3 << HERE
>  import mylib
>  pass
> HERE
> 
> function! bar()
> python3 << HERE
>  import mylib
>  pass
> HERE
> 
> The src says:
> 
>  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). Mixed bindings?
> 
> How many times is mylib compiled to bytecode and loaded? Does each
> vimscript function get its own mylib - can I instantiate something and
> expect it to be visible in the other function? 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.

I'm not a vim user and I only got to work something similar with Python 2.
I put

print "hello"

into mylib.py

and saw when it was printed with the following script:

function! Foo()
python << HERE
import sys
sys.path.append(".")
import mylib
HERE
endfunction

function! Bar()
python << HERE
import mylib
HERE
endfunction

:so vimscript.txt
:call Foo()
--> hello
:call Bar()
--> (nothing)

Conclusion: both functions share the same Python interpreter.
With a function containing

import os
print os.getpid()

you can also see that the Python interpreter runs in the same process as 
vim.





More information about the Python-list mailing list