merits of Lisp vs Python

Robert Brown bbrown at speakeasy.net
Tue Dec 12 00:12:13 EST 2006


greg <greg at cosc.canterbury.ac.nz> writes:
> From another angle, think about what a hypothetical Python-to-Lisp
> translator would have to do. It couldn't just translate "a + b" into
> "(+ a b)". It would have to be something like "(*python-add* a b)" where
> *python-add* is some support function doing all the dynamic dispatching
> that the Python interpreter would have done.

Luckily, Willem Broekema has written a Python to Lisp compiler called
clpython that can be consulted to answer questions like these.

    http://trac.common-lisp.net/clpython/

It turns out that addition is fairly straightforward.  Attribute lookup,
however, turns out to be complex, as is comparing objects.  Here is Willem's
description of the attribute lookup algorithm from the file doc/pres.txt:


   To look up person.name (the "name" attribute of object "person")

   a. if the class of "person" (say, Person), or one of Person's
      base classes (say, Animal) defines __getattribute__,
      that will intercept all attribute lookups.
      Call:  Animal.__getattribute__(person, name)

   b. look in instance dictionary:  val = person.__dict__["name"]

        - but if it has fixed slots:
          look in person.__slots__
           and give error if "name" is not one of the fixed slots

           - unless "__dict__" is specified as one of the fixed slots:
             in that case, don't give an error if it is not one of the
             fixed slots, but search in the instance dictionary too

   c. look in the classes Person, Animal for an attribute called "name"

       - if it is a `descriptor', call its __get__ method
       - else, if it is a method, make it a bound method
       - else, return it unchanged

   d. if nothing found so far: look for __getattr__ method in the
      classes, and call it:  C.__getattr__(person, "name")



More information about the Python-list mailing list