Friday, January 28, 2011

The Philosophy of PyPy...roughly

This has been gleaned from the excellent PyPy site, just so I could understand why PyPy exists and the goals for it.

This is how Python currently runs programs.





Suppose the interpreter is in Python instead of C. Ignoring performance, this would be a great setup since we could work in Python rather than C. But we cant ignore performance! This setup would be slow.




So if we have to get back to the speed levels of the existing CPython setup, we would have run an interpreter based in C rather than in Python. How about we keep the source file of the interpreter in Python but use a translator to convert the code to C? Then we can still write the interpreter in Python and also get a functioning interpreter that’s fast as C.




But are we sure that a translated interpreter will always be as fast as or faster than the original C interpreter? Are there other ways we can increase the speed of this setup? Yes! Instead of just a translator, use the JIT (just-in-time) compiler-generator to generate a JIT compiler out of the Python interpreter.



Points to note:

  • The interpreter written in Python in PyPy is actually written in RPython, a restricted version of Python that is statically-typed. This is to enable generation of a more-efficient program for the target platform.
  • The JIT Compiler generated uses a different approach than the JIT compiler that can be generated for the CPython version (Psyco) and according to the PyPy site has demonstrated superior performance, even though its still a work in progress.

Thursday, January 20, 2011

Python introspection

This is a handy article on python introspection. The article is old (Dec 2002) and uses Python 2.2 but you can work through the article with the current Python version which is 2.7. There are a few differences, for eg. two new keywords "as" and "with" will be displayed in the exercise which lists keywords, but most of the exercises are useful to get an understanding of Python's introspection abilities.

http://www.ibm.com/developerworks/library/l-pyint.html

One thing to note: in listing 29 (Types), its worth mentioning that if you use the 'type' function on modules , it only works for modules that are loaded. For modules that are not, you get an error. Calling type on built-ins will not give an error since built-ins like int are always loaded on initialization. Here's an example:


Python 2.6.3 (r263rc1:75186, Oct 2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(int)
< type 'type' >
>>> type(sys)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'sys' is not defined
>>> import sys
>>> type(sys)
< type 'module' >
>>>