选 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 库。

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

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

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.