4 tools for code conversion between Python and JavaScript

高洛峰
Release: 2016-11-23 13:25:01
Original
1260 people have browsed it

Python or JavaScript? Although many friends are still debating which of the two is currently stronger and who has a brighter development prospect, there is no doubt that the competition between the two has a clear answer in the field of Web front-end. Based on the browser platform, if we abandon JavaScript, we will have few projects to choose from.

Okay, maybe the answer is not so absolute. JavaScript has become the conversion target of choice for many other programming languages ​​(examples include TypeScript, Emscripten, Cor, and Cheerp). Python, on the other hand, has a huge following and a strong library of existing libraries, making it an ideal conversion or translation option for JavaScript.

Let’s take a look at four projects that can successfully bring Python into the JavaScript world:

Transcrypt

This is a newly emerged Python to JS translator. Transcrypt has an impressive commitment to the quality level of the code it generates. First, it preserves as much of the original Python code structure as possible, including multiple inheritance and lambda expressions. Python source code can also directly call objects in the namespace in JavaScript. If you try to access document.getElementById in Python, the converted code will also switch to using document.getElementById in JavaScript.

According to the documentation, Transcrypt uses CPython’s abstract syntax tree module to complete these translation tasks, which can be accessed programmatically based on how Python parses its own code. Although the project is still in alpha testing stage, it is already showing amazing traction.

Jiphy

The so-called Jiphy stands for "JavaScript in, Python out" - that is, it can convert the two in both directions. Additionally, code from both languages ​​can be mixed before being converted to the other language.

The biggest shortcoming of Jiphy right now is that it only supports part of Python’s feature set. Classes and default parameters are not yet supported, but decorators and exceptions are available. This is mainly because Jiphy insists on direct line-to-line translation between source code and target code, but its developers are also starting to look at new features in ES6 to support more advanced Python features.

Brython

Maybe one day, when the vision of WebAssembly becomes a reality, then we will be able to choose any language of our preference for web development. And Brython has its own understanding of this—or at least it applies to Python 3: Why wait?

Brython achieves the goal of using the Python 3 version as a client-side web programming solution by emulating all keywords in Python 3 and most of the built-in plug-ins through a set of JavaScript libraries. Scripts written in Python can be added directly to web pages, and Brython also supports a set of advanced Python module interfaces (browser) for execution collaboration with the DOM, which can often be done directly in JavaScript.

However, Brython also maintains the limitations that browsers impose on JavaScript code - such as not supporting local file system processing.

RapydScript

RapydScript promises to "make Pythonic JavaScript code no longer suck." The project is similar in concept to CoffeeScript: code in Python, generate JavaScript code, and bring out the best of both. On the Python side, it has clear syntax rules; on the JavaScript side, it has anonymous functions, DOM manipulation, and the ability to use existing JavaScript libraries such as jQuery or the Node.js core.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!