Monday, March 21, 2016

Re: Vim Function: return from within Python Code

2016-03-19 19:38 GMT+03:00 rameo <raiwil@gmail.com>:
> I have a VIM function with a lot of code written in python.
>
> There are many loops and sub-loops with this kind of code:
>
> if this
> exit
> else
> do this
>
> Does anyone know how to exit the python code and return to vim?
> (or even better "how to exit the function from within python code?")
> sys.exit() and exit() doesn't work.

It is better to wrap everything into a function placed in a Python
module placed somewhere in {runtimepath}/python2. Note that whatever
you have written in your big function *it is placed in the global
scope*, available by every plugin. :pyfile command is by no means
different, it is literally using :python command on the whole file. So
best practice is a separate module (which has its own scope) with only
a few imported things (either some function from the module or module
itself) which all contain plugin name as a prefix: everything to avoid
possible name conflicts or breakage of plugins which use Python, but
do not follow best practices. Module is imported using a regular
`:python import myplugin` or `:python from myplugin import foo as
myplugin_foo` command, no :pyfile.

In addition to regular early return for exiting from the deep
functions hierarchy there are exceptions, but I do not remember any
time I needed to use them as a longjump except for breaking in case of
error. Needs try/except at the top, but this is easy to do.

>
>
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: