Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Describes the difference between pyjamas code and native python code.
This document applies to Pyjamas 0.6.
There are some translator options that make the generated code more or less pythonic. The two most important are -O and --strict. The first optimizes for speed and omits e.g. attribute checking. The latter tries to get as close as it can to the http://python.org behavior. If you're building a library, try to make it work with --strict _and_ without --strict.
As a general rule, readers are advised to themselves build the pyjamas regression tests using the compiler options of their choice (see pyjsbuild --help) and to check the individual regression tests to see which python language features they require. At the time of writing, there are over 1,600 individual regression tests, in over 20 modules. If a particular python feature is not supported, readers are advised to submit a feature request at http://code.google.com/p/pyjamas/issues submitting a patch which includes a simple test for the feature requested.
This used to also applies to classes: for Pyjamas 0.5 and before, if you were passed a class object and you wanted to instantiate it, you needed to use cls.new() to create an instance of the class. However, for Pyjamas 0.6, as can be seen in libtest ClassTest.py testGlobalFactory, even with -O it is now possible to instantiate classes by passing the class object as a variable, and calling it.
Attribute Access and Bound Methods
With the option --bound-methods (also enabled with --strict), methods are autometically "bound" to a class instance. Without this option, methods are not automatically "bound" to the object when you access them (unless you do so by calling the method). To get a pointer to a "bound" method, use getattr(obj, "methodName"). This technique results in anythin up to a 100% speed increase over --strict, depending on the browser.
Class methods, static methods, unbound methods, and class variables are all emulating the python behavior quite well at this time. However, be aware that pyjamas is sensitive to capitalization, and there may be some issues if you have classes that start with a lower case letter.
Alternatively, the --operator-funcs (and --strict) option now enables creation of code where "+" is replaced by obj1.__add__(obj2), "-" with __sub__ etc., and string, integer and list objects now have __add__, __sub__, __mul__ etc.
A workaround for 3 << 34 (which is 12 in JS) is 3 * Math.pow(2,34), or in Pyjamas: 3 * (234) For 51539607552 >> 34 is the Pyjamas workaround: 3 / (234)
51539607552 | 3 == 3 (should be 51539607555) 51539607556 | 3 == 7 (should be 51539607559)
There's no string multiplication (e.g. 'a' * 4, or 'abc' * 2) A workaround for 'a' * 4 is: ''.ljust(4, 'a') A workaround for 'abc' * 2 is: ''.join(['abc' for i in range(2)])
Update: the --operator-funcs (and --strict) option now enables creation of code where "+" is replaced by obj1.__add__(obj2), "-" with __sub__ etc., and string, integer and list objects now have __add__, __sub__, __mul__ etc. so, "l = 'a' * 5" now works; as does "l = [1,2]; l += ".
List addition is not supported in Pyjamas (e.g. [1,2] + [3,4]) A workaround for a = [1,2];b = a + [3,4] is: b = a[:];b.extend([3,4])
Update: the --operator-funcs (and --strict) option now enables creation of code where "+" is replaced by obj1.__add__(obj2), "-" with __sub__ etc., and string, integer and list objects now have __add__, __sub__, __mul__ etc. so, "l =  * 5" now works; as does "l = [1,2]; l += ".
Tuple addition is not supported in Pyjamas (e.g. (1,2) + (3,4)) A workaround for a = (1,2);b = a + (3,4) is: b = list(a)[:];b.extend(list((3,4)));b=tuple(b)