Home > Web Front-end > JS Tutorial > body text

A detailed introduction to 4 tools for converting Python and JavaScript code

黄舟
Release: 2017-04-18 17:36:18
Original
3323 people have browsed it

Detailed introduction of 4 tools to achieve code conversion between Python and JavaScript:

Which one should choose 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 give up JavaScript, we will have few projects to choose from.

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

四款工具顺利实现 Python 与 JavaScript 间的代码转换

Let’s take a look at four projects that can successfully bring Python into the JavaScript world; and one of them stands out because of its flexible bidirectional conversion capabilities.

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 introduction of the documentation, Transcrypt uses CPython's abstract syntax tree module to complete these translation tasks, which can be accessed programmatically based on Python's way of parsing 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.

Jiphy’s current biggest shortcoming is that it only supports a 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

Perhaps one day, when the WebAssembly vision becomes a reality, 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 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 play two languages ​​simultaneously the best characteristics of the person. 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.

Recommended related tools:

A very useful JavaScript development tool: WebStorm Mac version

php Chinese website Free tools: php development tools download

The above is the detailed content of A detailed introduction to 4 tools for converting Python and JavaScript code. For more information, please follow other related articles on the PHP Chinese website!

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!