Re: Referring to variables in Python fragments

Bram Moolenaar <[email protected]> Fri, 05 Mar 2004 20:13:07 +0100
Newsgroups gmane.comp.tools.aap.devel
Message-ID <[email protected]>
John Williams wrote:

> I'm bothered by having to prefix all my recipe variables with "_no" in
> Python code; it's awkward to type, very strange looking for a beginner
> such as myself, and probably very easy to accidentally forget.

Right.  The only reason it's this way is that we could not make it work
like we wanted to.  The "exec" command does not accept a user
dictionary.

> I have an alternate suggestion: allow the "$variable" syntax in Python
> code.  As far as I can tell, this would just mean replacing "$" with
> "_no." outside of string literals.

That does sound like a nice simplification.  Doesn't Python use $
outside of strings?  I can't think of anything.  One catch: It could be
used in the next Python version.

> Taking this a step futher, the "$" could work in string literals, too, so
> for example, "the value of X is $X" could become ("the value of X is " +
> _no.X).

That does require escaping litaral "$" characters.  I don't think we
should go this way, since using "%s" isn't difficult, while escaping can
get complicated.

> Another possible improvement would be to allow some of the extended
> sytnaxes like $?var and $*var in Python code.

Support $?var would not be much of a problem.  Using "_no.get('var')"
should work.  Things like $_parent.var can work as well (just to make it
consistent, the $ isn't needed here).

Implementing $*var is complicated.  This requires parsing what comes
before and after it.  I wouldn't do that, try to keep it simple.

> If I wrote a patch for this would it be accepted?

The question is if this works perfectly well in all situations.  We need
to parse the Python code to discover where the "$" must be replaced with
"_no.".  How complicated is this?

If you can write a patch that looks like it should work, with a test,
and nobody objects to this use of "$" in Python code, I'll probably
include it.


PS: I had to manually approve your messages.  Are you not subscribed to
the maillist or using a different address?

-- 
Snoring is prohibited unless all bedroom windows are closed and securely
locked.
		[real standing law in Massachusetts, United States of America]

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\  Buy at Amazon and help AIDS victims -- http://ICCF.nl/click1.html ///


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click