[Python-ideas] Towards harmony with JavaScript?

Alberto Berti alberto at metapensiero.it
Sat Aug 12 07:04:45 EDT 2017


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

    Chris> On Sat, Aug 12, 2017 at 6:31 AM, Alberto Berti <alberto at metapensiero.it> wrote:

    >> 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.

    Chris> Do you "retain most of Python language semantics", or do you "always
    Chris> manipulate JS objects"? As shown in a previous post, there are some
    Chris> subtle and very dangerous semantic differences between the languages.
    Chris> You can't have it both ways.

that's right you can't have both ways. That's the difficult decision to
make, because as you add more and more Python APIs to those supported,
probably you'll end up creating your "Python island in JS" where you
need to transform the objects you manipulate from/to JS on the functions
that are called by external JS code (either manually or automatically).

And on the other end, if don't add any Pythonic API you will end up with
ugly Python code that yes, is valid Python code, but that's nothing you
would like to see.

JavaScripthon was and is an experiment to see how much of the "Pythonic
way of expressing alogrithms" can be retained adding as less "runtime"
as possible. That's the reason why it targets ES6+ JavaScript, because
the "point of contacts" between the two languages are much higher in
number.

As an example let's take the following simple code:

  def test():
      a = 'foo'   
      d = {a: 'bar'}
      return d[a]
    
one can naively translate it to:

  function test() {
      var a, d;
      a = 'foo';
      d = {a: 'bar'};
      return d[a];
  }

but it returs 'bar' in Python and undefined in JS. But even if it's just
a simple case expressed in a four lines function, it one of those things
that can slip through when coding in both languages at the same time (at
least for me). So I asked myself if it was worthwhile to have a tool
that:

* allows me to use Python syntax to write some amount of JS code. I'm
  more accustomed to Python syntax and I like it more. It's generally
  more terse and has less distractions (like variable declarations and
  line terminations);

* fixes as many as possible of this things automatically, without having
  to precisely remember that this is a "corner case" in JS and that must
  be handled with care (so that reduces the "context-switching" effort);

* produces a good looking JS code that's still possible to read and
  follow without much trouble.

How many "corner cases" like this there are in JS? In my coding
experience, "thanks" to the fact that JS is much less "harmonious" than
Python ( my opinion ) I've encountered many of those, and also there are
many simple Python coding habits that are translatable in a simple
way. So what the tools does in this case?

 $ pj -s -
   def test():
       a = 'foo'   
       d = {a: 'bar'}
       return d[a]

 function test() {
     var a, d;
     a = "foo";
     d = {[a]: "bar"};
     return d[a];
 }

It turns out that ES6 has a special notation for wath JS calls "computed
property names", keys in object literals that aren't strings

does it evaluates the way a Python developer expects when run? let's see

$ pj -s - -e
  def test():
      a = 'foo'   
      d = {a: 'bar'}
      return d[a]
  test()
  
bar



More information about the Python-ideas mailing list