This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

some Python Zen (Re: Python transformer for Guile?)


I consider myself a ``python master'' FWIW - perhaps not yet
``expert'', but I get along quite well.

So I thought I would share some drops of Python Zen here, to
cheer up the people trying to write the translator if python's
parser can't be used.


The first thing to know is that 90% of Python is syntatic
sugar. Python is the Syntatic Sugar Language per excellence.

So one possible approach would be a two-pass translator; in the
first pass, it removes all the syntatic sugar, and in the
second pass it generates scheme code from the results.

When you say ``foo[bar]'', what python really does is
``foo.__getitem__(bar)''. (At least in theory; there are
probably some optimizations involved with base types.) This
maps a lot more nicely to Scheme.

Ditto for ``foo.bar''; you can express it as
``getattr(foo, 'bar')''.


The next point is that every language has a key-element, a
concept that must be grasped and around which the whole
language revolves. In C, it's the function (duh); in Scheme,
it's the list (actually, the pair, but this is even more Zen
than the rest; if you try to understand it thinking of pairs,
you'll get insane. If you think in terms of lists, you'll be
enlightened and see the pairs.)

In Python the basic unit is the Dictionary (mapping, or alist).
Sequences are optimized dictionaries. Attributes are stored in
dictionaries (every non-base object has a __dict__ attribute
that points to the dictionary which is actually the internal
representation of the object). BTW, the class is also stored in
a ``hidden'' attribute - __class__. This attribute is used to
dispatch messages (method calls).


One more interesting, but not necessarily relevant, point is
that any object can pretend to be a function by just having a
``__call__'' method (actually, a ``__call__'' attribute which
is itself a function).


And that's pretty much all there is to it.

[]s,
                                               |alo
                                               +----
--
      I am Lalo of deB-org. You will be freed.
                 Resistance is futile.

http://www.webcom.com/lalo      mailto:lalo@webcom.com
                 pgp key in the web page

Debian GNU/Linux       ---       http://www.debian.org
Brazil of Darkness   --   http://zope.gf.com.br/BroDar


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]