Home Backend Development Python Tutorial Python和JavaScript间代码互转的4个工具

Python和JavaScript间代码互转的4个工具

Jun 10, 2016 pm 03:06 PM

选 Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势、谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案。立足于浏览器平台,如果放弃 JavaScript,我们也就没什么可选择的项目了。

好吧,也许答案也不是这么绝对。JavaScript 已经成为众多其它编程语言争相选择的转换目标(相关实例包括 TypeScript、Emscripten、Cor 以及 Cheerp)。而 Python 则拥有庞大的追随者群体,另外现有的强大库资源则使其成为面向 JavaScript 的理想待转换或者说转译选项。

下面来看四个能够顺利将 Python 带入 JavaScript 世界的项目; 而其中一款更是凭借着灵活的双向转换能力而鹤立鸡群。

Transcrypt

这是一款新近崛起的 Python 到 JS 转译器。Transcrypt 对于自身所生成代码的质量水平做出了令人印象深刻的承诺。首先,它会尽可能多地保留原始的 Python 代码结构,包括多重继承以及 lambda 表达式。Python 源代码也可以直接对 JavaScript 中命名空间内的对象进行调用。如果大家尝试访问 Python 中的 document.getElementById,则转换后的代码也将在JavaScript 当中切换使用 document.getElementById。

根据说明文档的介绍,Transcrypt 是利用 CPython 的抽象语法树模块完成这些转译任务的,其能够根据 Python 对自身代码的解析方式进行编程访问。尽管该项目目前仍处于 alpha 测试阶段,但已经显示出了非常惊人的吸引力。

Jiphy

所谓 Jiphy,代表的是“JavaScript 入,Python 出”——也就是能够对二者进行双向转换。另外,来自两种语言的代码都能够在被转换为另一种语言之前进行混合。

Jiphy 目前的最大短板在于其仅支持 Python 的一部分功能集。类以及默认参数尚不受支持,不过装饰器与例外机制已经可以正常使用。这主要是因为 Jiphy 坚持在源代码与目标代码之间采用行对行直接转译方式,不过其开发人员也开始着眼于 ES6 中的新功能,旨在将更多高级 Python 功能纳入支持范畴。

Brython

也许有一天,当 WebAssembly 设想成为现实,那么我们将能够选择任何自己偏好的语言进行 Web 开发。而 Brython 对此——或者说至少适用于 Python 3——有着自己的理解:为什么要等?

Brython 通过一套 JavaScript 库对 Python 3 中的全部关键字以及大多数内置插件进行模拟,从而实现了将 Python 3 版本作为客户端 Web 编程方案的目标。由 Python 编写的脚本可以被直接添加到网络页面当中,而 Brython 还支持一套高级 Python模块界面(browser),用于同 DOM 进行执行协作,且该浏览器通常可在 JavaScript 中直接完成。

然而,Brython 也保持了浏览器给 JavaScript 代码带来的限制——例如不支持对本地文件系统进行处理。

RapydScript

RapydScript 承诺“让 Python 式 JavaScript 代码不再糟糕。”该项目在概念上类似于 CoffeeScript:以 Python 形式进行代码编写,生成 JavaScript 代码,并同时发挥二者的最佳特性。在 Python 方面,其拥有清晰的语法规则; 而在 JavaScript 方面,其拥有匿名函数、DOM 操作并能够使用 jQuery 或者 Node.js 内核等现有 JavaScript 库。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles