Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80253

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

From Peter Otten <__peter__@web.de>
Subject Re: Python, VIM: namespace, scope, life of a python object stuck in a << HERE
Date 2014-11-05 08:44 +0100
Organization None
References <m3chpj$7qk$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.15482.1415173467.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Python, VIM: namespace, scope, life of a python object stuck in a << HERE Veek M <vek.m1234@gmail.com> - 2014-11-05 12:28 +0630
  Re: Python, VIM: namespace, scope, life of a python object stuck in a << HERE Peter Otten <__peter__@web.de> - 2014-11-05 08:44 +0100

csiph-web