1: IronPython
- IronPython是一个开源的Python实现,专为.NET和Mono设计,使用C#编写,采用Apache License 2.0授权。它依赖于DLR(动态语言运行时)。目前仅支持Python 2.7版本,Python 3版本正在开发中。
- 与CPython的差异:
- 与.NET Framework紧密集成。
- 默认情况下,字符串是Unicode。
- 不支持用C语言为CPython编写的扩展。
- 不受全局解释器锁(GIL)的限制。
- 性能通常较低,但具体取决于测试。
Hello World
print "Hello World!"
你也可以使用.NET函数:
import clr
from System import Console
Console.WriteLine("Hello World!")
外部链接
- 官方网站
- GitHub仓库
2: Jython
Jython是一个开源的Python实现,专为JVM设计,使用Java编写,采用Python软件基金会许可。目前仅支持Python 2.7版本,Python 3版本正在开发中。
与CPython的差异:
- 与JVM紧密集成。
- 字符串是Unicode。
- 不支持用C语言为CPython编写的扩展。
- 不受全局解释器锁(GIL)的限制。
- 性能通常较低,但具体取决于测试。
Hello World
print "Hello World!"
你也可以使用Java函数:
from java.lang import System
System.out.println("Hello World!")
外部链接
- 官方网站
- Mercurial仓库
3: Transcrypt
Transcrypt是一个工具,可以将Python的一个相当广泛的子集预编译为紧凑且可读的JavaScript。它具有以下特点:
- 使用纯Python语法进行经典面向对象编程,支持多重继承,由CPython的原生解析器解析。
- 与高质量的Web导向JavaScript库无缝集成,而不是桌面导向的Python库。
- 基于层次结构的URL模块系统,允许通过PyPi分发模块。
- Python源代码与生成的JavaScript代码之间有简单的对应关系,便于调试。
- 多级源码映射和可选的目标代码注释,包含源代码引用。
- 紧凑的下载包,以KB为单位,而不是MB。
- 优化的JavaScript代码,使用记忆化(调用缓存)可选地绕过原型查找链。
- 可以局部开启或关闭运算符重载,便于进行可读的数值计算。
代码大小和速度
经验表明,650KB的Python源代码大致转换为相同量的JavaScript源代码。速度与手写的JavaScript相当,并且如果开启调用记忆化,速度可以超过手写的JavaScript。
与HTML集成
<script src="__javascript__/hello.js"></script>
Hello demo
...
...
与JavaScript和DOM集成
from itertools import chain
class SolarSystem:
planets = [list (chain (planet, (index + 1,))) for index, planet in enumerate ((
('Mercury', 'hot', 2240),
('Venus', 'sulphurous', 6052),
('Earth', 'fertile', 6378),
('Mars', 'reddish', 3397),
('Jupiter', 'stormy', 71492),
('Saturn', 'ringed', 60268),
('Uranus', 'cold', 25559),
('Neptune', 'very cold', 24766)
))]
lines = (
'{} is a {} planet',
'The radius of {} is {} km',
'{} is planet nr. {} counting from the sun'
)
def __init__ (self):
self.lineIndex = 0
def greet (self):
self.planet = self.planets [int (Math.random () * len (self.planets))]
document.getElementById ('greet') .innerHTML = 'Hello {}'.format (self.planet [0])
self.explain ()
def explain (self):
document.getElementById ('explain').innerHTML = (
self.lines [self.lineIndex] .format (self.planet [0], self.planet [self.lineIndex + 1])
)
self.lineIndex = (self.lineIndex + 1) % 3
solarSystem = SolarSystem ()
与其他JavaScript库集成
Transcrypt可以与任何JavaScript库结合使用,无需采取特殊措施或语法。文档中给出了与React.js、Riot.js、Fabric.js和Node.js等库结合使用的示例。
Python与JavaScript代码的关系
Python代码:
class A:
def __init__ (self, x):
self.x = x
def show (self, label):
print ('A.show', label, self.x)
class B:
def __init__ (self, y):
alert ('In B constructor')
self.y = y
def show (self, label):
print ('B.show', label, self.y)
class C (A, B):
def __init__ (self, x, y):
alert ('In C constructor')
A.__init__ (self, x)
B.__init__ (self, y)
self.show ('constructor')
def show (self, label):
B.show (self, label)
print ('C.show', label, self.x, self.y)
a = A (1001)
a.show ('america')
b = B (2002)
b.show ('russia')
c = C (3003, 4004)
c.show ('netherlands')
show2 = c.show
show2 ('copy')
JavaScript代码:
var A = __class__ ('A', [object], {
get __init__ () {return __get__ (this, function (self, x) {
self.x = x;
});},
get show () {return __get__ (this, function (self, label) {
print ('A.show', label, self.x);
});}
});
var B = __class__ ('B', [object], {
get __init__ () {return __get__ (this, function (self, y) {
alert ('In B constructor');
self.y = y;
});},
get show () {return __get__ (this, function (self, label) {
print ('B.show', label, self.y);
});}
});
var C = __class__ ('C', [A, B], {
get __init__ () {return __get__ (this, function (self, x, y) {
alert ('In C constructor');
A.__init__ (self, x);
B.__init__ (self, y);
self.show ('constructor');
});},
get show () {return __get__ (this, function (self, label) {
B.show (self, label);
print ('C.show', label, self.x, self.y);
});}
});
var a = A (1001);
a.show ('america');
var b = B (2002);
b.show ('russia');
var c = C (3003, 4004);
c.show ('netherlands');
var show2 = c.show;
show2 ('copy');
外部链接
- 官方网站
- GitHub仓库