By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,578 Members | 825 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Non-official Python Implementations

P: 1
Python Implementations.jpgPython is an interpreted, interactive, object-oriented programming language. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants, on the Mac, and on Windows 2000 and later.

Python Implementations

An "implementation" of Python should be taken to mean a program or environment which provides support for the execution of programs written in the Python language, as represented by the CPython reference implementation.

There have been and are several distinct software packages providing of what we all recognize as Python, although some of those are more like distributions or variants of some existing implementation than a completely new implementation of the language.
  1. IronPython
  2. Jython
  3. Transcrypt

IronPython

Open-source implementation for .NET and Mono written in C#, licensed under Apache License 2.0. It relies on DLR
(Dynamic Language Runtime). It supports only version 2.7, version 3 is currently being developed.

Differences with CPython:
  • Tight integration with .NET Framework.
  • Strings are Unicode by default.
  • Does not support extensions for CPython written in C.
  • Does not suffer from Global Interpreter Lock.
  • Performance is usually lower, though it depends on tests.

Hello World

Expand|Select|Wrap|Line Numbers
  1. print "Hello World!"
You can also use .NET functions:

Expand|Select|Wrap|Line Numbers
  1. import clr
  2. from System import Console
  3. Console.WriteLine("Hello World!")
External links
  • Official website
  • GitHub repository

Jython

Open-source implementation for JVM written in Java, licensed under Python Software Foundation License. It supports only version 2.7, version 3 is currently being developed.

Differences with CPython:
  • Tight integration with JVM.
  • Strings are Unicode.
  • Does not support extensions for CPython written in C.
  • Does not suffer from Global Interpreter Lock.
  • Performance is usually lower, though it depends on tests.

Hello World

print "Hello World!"

You can also use Java functions:

Expand|Select|Wrap|Line Numbers
  1. from java.lang import System
  2. System.out.println("Hello World!")
  3.  
External links
  • Official website
  • Mercurial repository

Transcrypt

Transcrypt is a tool to precompile a fairly extensive subset of Python into compact, readable Javascript. It has the following characteristics:
  • Allows for classical OO programming with multiple inheritance using pure Python syntax, parsed by
    CPythonís native parser
  • Seamless integration with the universe of high-quality web-oriented JavaScript libraries, rather
    than the desktop-oriented Python ones
  • Hierarchical URL based module system allowing module distribution via PyPi
  • Simple relation between Python source and generated JavaScript code for easy debugging
  • Multi-level sourcemaps and optional annotation of target code with source references
  • Compact downloads, kBís rather than MBís
  • Optimized JavaScript code, using memoization (call caching) to optionally bypass the prototype
    lookup chain
  • Operator overloading can be switched on and off locally to facilitate readable numerical math

Code size and speed

Experience has shown that 650 kB of Python source code roughly translates in the same amount of JavaScript source code. The speed matches the speed of handwritten JavaScript and can surpass it if call memoizing is switched on.

Integration with HTML

Expand|Select|Wrap|Line Numbers
  1. <script src="__javascript__/hello.js"></script>
  2. <h2>Hello demo</h2>
Expand|Select|Wrap|Line Numbers
  1. <p>
  2. <div id = "greet">...</div>
  3. <button onclick="hello.solarSystem.greet ()">Click me repeatedly!</button>
Expand|Select|Wrap|Line Numbers
  1. <p>
  2. <div id = "explain">...</div>
  3. <button onclick="hello.solarSystem.explain ()">And click me repeatedly too!</button>
Integration with JavaScript and DOM

class SolarSystem:

Expand|Select|Wrap|Line Numbers
  1. planets =[list (chain (planet, (index + 1,))) for index, planet in enumerate ((
  2.  ('Mercury', 'hot', 2240),
  3.  ('Venus', 'sulphurous', 6052),
  4.  ('Earth', 'fertile', 6378),
  5.  ('Mars', 'reddish', 3397),
  6.  ('Jupiter', 'stormy', 71492),
  7.  ('Saturn', 'ringed', 60268),
  8.  ('Uranus', 'cold', 25559),
  9.  ('Neptune', 'very cold', 24766)
  10.  ))]
Expand|Select|Wrap|Line Numbers
  1. lines = (
  2.  '{} is a {} planet',
  3.  'The radius of {} is {} km',
  4.  '{} is planet nr. {} counting from the sun'
  5.  )
Expand|Select|Wrap|Line Numbers
  1. def __init__ (self):
  2.  self.lineIndex = 0
Expand|Select|Wrap|Line Numbers
  1. def greet (self):
  2.  self.planet = self.planets [int (Math.random () * len (self.planets))]
  3.  document.getElementById ('greet') .innerHTML = 'Hello {}'.format (self.planet [0])
  4.  self.explain ()
Expand|Select|Wrap|Line Numbers
  1.  def explain (self):
  2.  document.getElementById ('explain').innerHTML = (
  3.  self.lines [self.lineIndex] .format (self.planet [0], self.planet [self.lineIndex + 1])
  4.  )
  5.  self.lineIndex = (self.lineIndex + 1) % 3
  6.  solarSystem = SolarSystem ()
  7.  
Integration with other JavaScript libraries

Transcrypt can be used in combination with any JavaScript library without special measures or syntax. In the documentation, examples are given for a.o. react.js, riot.js, fabric.js and node.js.

Relation between Python and JavaScript code

Python

Expand|Select|Wrap|Line Numbers
  1. class A:
  2.  def __init__ (self, x):
  3.  self.x = x
  4.  def show (self, label):
  5.  print ('A.show', label, self.x)
  6.  
  7. class B:
  8.  def __init__ (self, y):
  9.  alert ('In B constructor')
  10.  self.y = y
  11.  
  12.  def show (self, label):
  13.  print ('B.show', label, self.y)
  14.  
  15. class C (A, B):
  16.  def __init__ (self, x, y):
  17.  alert ('In C constructor')
  18.  A.__init__ (self, x)
  19.  B.__init__ (self, y)
  20.  self.show ('constructor')
  21.  
  22.  def show (self, label):
  23.  B.show (self, label)
  24.  print ('C.show', label, self.x, self.y)
  25.  
  26. a = A (1001)
  27. a.show ('america')
  28. b = B (2002)
  29. b.show ('russia')
  30. c = C (3003, 4004)
  31. c.show ('netherlands')
  32. show2 = c.show
  33. show2 ('copy')
JavaScript

Expand|Select|Wrap|Line Numbers
  1. var A = __class__ ('A', [object], {
  2.  get __init__ () {return __get__ (this, function (self, x) {
  3.  self.x = x;
  4.  });},
  5.  get show () {return __get__ (this, function (self, label) {
  6.  print ('A.show', label, self.x);
  7.  });}
  8. });
  9. var B = __class__ ('B', [object], {
  10.  get __init__ () {return __get__ (this, function (self, y) {
  11.  alert ('In B constructor');
  12.  self.y = y;
  13.  });},
  14.  get show () {return __get__ (this, function (self, label) {
  15.  print ('B.show', label, self.y);
  16.  });}
  17. });
  18. var C = __class__ ('C', [A, B], {
  19.  get __init__ () {return __get__ (this, function (self, x, y) {
  20.  alert ('In C constructor');
  21.  A.__init__ (self, x);
  22.  B.__init__ (self, y);
  23.  self.show ('constructor');
  24.  });},
  25.  get show () {return __get__ (this, function (self, label) {
  26.  B.show (self, label);
  27.  print ('C.show', label, self.x, self.y);
  28.  });}
  29. });
  30. var a = A (1001);
  31. a.show ('america');
  32. var b = B (2002);
  33. b.show ('russia');
  34. var c = C (3003, 4004);
  35. c.show ('netherlands');
  36. var show2 = c.show;
  37. show2 ('copy');
Mar 13 '19 #1
Share this Article
Share on Google+
1 Comment


libreusl
P: 2
Useful. Included one I haven't heard of yet.

I'm following PyPy and very possibly migrating to it from cpython eventually.
Mar 28 '19 #2