[Python-ideas] Towards harmony with JavaScript?

Alberto Berti alberto at metapensiero.it
Fri Aug 11 15:13:33 EDT 2017


>>>>> "Chris" == Chris Angelico <rosuav at gmail.com> writes:

    Chris> On Sat, Aug 12, 2017 at 3:04 AM, Alberto Berti <alberto at metapensiero.it> wrote:
    >> For the goal of reducing the friction (the mind switching when working
    >> with both the languages) I have created a tool (
    >> https://github.com/azazel75/metapensiero.pj ) which allows me to write
    >> valid Python and translates this to nice JS while taking care of most of
    >> these nuances. At the same time it doesn't raise any barrier between the
    >> translated code and any other JS library around (and I use them a lot).

    Chris> What do you do about all the places where the languages have
    Chris> significantly different semantics? For instance, a Python integer can
    Chris> store more values than a Python float (which is broadly compatible
    Chris> with a JS Number), but in JS, bitwise operations restrict the value to
    Chris> 32-bit.

As of now, I do nothing. As I said, the goal of the tool is not to
shield you from JS, for this reason it's not meant for beginners (in
both JS or Python). You always manipulate JS objects, but allows you to
to be naive on all that plethora of JS idiosyncrasies (from a Python pow
at least) that you have to think about when you frequently switch from
python to js.

Because such list of idiosyncrasies may be subjective, I hope to add to
it a kind of "layered" translation where the user can add its own set of
rules and/or  runtime checking or conversion. I've helped porting
macropy ( https://github.com/azazel75/macropy ) especially for the
purpose of simplify AST manipulation, but it's not done yet.

    Chris> And subscripting or iterating over a string containing astral
    Chris> (non-BMP) characters will do different things.

This is strange... i tested it with javascripthon embedded interpreter
(which is ES5 compatible) and it indeed shows that for example the
string '😼😼😼😼😼' isn't correctly (in the python sense) iterated over
but testing it on an ES6 compatible interpreter (more or less latest V8)
does the right thing. Something has changed between the two.

    Chris> Or when you use
    Chris> non-string keys in a dictionary (notably integers).

you should use a Map in such case, as the tool doesn't
reimplement most of the Python data api. That would mean build some
wrapping type that maybe would simplify converting existing Python code
to run on JS, but that would probably mean a more difficult interfacing
with JS third party libraries (React, Angular, name your here) that i
don't want to reimplement... See here (
https://github.com/azazel75/metapensiero.pj/issues/19 ) for a brief
discussion on this matter.

           
    Chris> Transpiling is an
    Chris> extremely dangerous thing to do a partial job of.

Even breathing can be dangerous in some environments...
Bridging two different concepts together is always a partial job...

Again, the use case may seem minimal to you but I can assure that it
helps on the day to day work with JS.



More information about the Python-ideas mailing list