class vs function ???

Stephen Horne steve at ninereeds.fsnet.co.uk
Sun Feb 22 03:26:48 EST 2004


On 21 Feb 2004 22:26:14 -0800, gveda at iitk.ac.in (Gaurav Veda) wrote:

>My basic doubts were :
>1) I am allowed to write things in the class body without the need for
>encapsulating them in functions. Moreover, they are executed as soon
>as I define the class. Infact, I can also call functions in the class
>body (the way  I called fun2). All this, while I am just defining the
>class !	-- I still don't understand why are classes implemented in
>this way in pyhton.

Well, I think of it this way. Some time back in ancient prehistory,
Python didn't have classes - but it did have simple imperitive code.
At some point, someone needed to add classes in. The options were...

1.  Make a minimal change, exploiting the existing functionality to
    allow quite sophisticated classes to be defined relatively easily
    using normal code and concepts that were already familiar to
    Python users and which could potentially benefit from updates to
    the core language.

2.  Treat class bodies as a completely new thing, and define a new
    'sublanguage' for them from the ground up which, once developed,
    Python users would have to learn as an extra thing in addition
    to the existing non-class aspects of Python, and which could not
    benefit from future useful additions to the core language unless
    their implementation was explicitly duplicated in the class
    definition sublanguage.

The first choice might seem odd if your experience up to now is mostly
of statically typed languages (where it isn't really an option) but
for a scripting language like Python, IMHO it is the most rational
choice.

>PS : No Steve, I am not trying out any tricks here. Believe me, I know
>too little to do any of that :)

OK, I guess I'll accept that - but I'm still keeping my eye on you <g>



BTW Python itself is already a compiler - a bytecode compiler - so if
you really need to write your own compiler I'd suggest taking a look
at how the PYC files are generated, and seeing if you can just wrap
them (or something similar) in an exe that embeds the Python
interpreter (or a subset of it, including at least the bytecode
interpreter and its dependencies). Keep in mind that converting a
sequence of text statements into a sequence of function calls (doing
little more than parsing and linearising expressions) may be a bit
simplistic, but it works - and doing anything noticably more
sophisticated would probably be pretty hard work.

I assume that's the approach taken by the existing Py2exe
(http://starship.python.net/crew/theller/py2exe/index.html).

If you need to write something more advanced, using static analysis to
do special case optimisations when possible, there was a project doing
something similar a little while back but I forget the name. I've done
some googling and keep finding references to Viperc, but I don't think
that's the one I'm thinking of.

If you can find it (whatever it's called) I imagine you'll find it
pretty clueful too.

If your aiming for more of a Python-like but not entirely compatible
language, Pyrex (http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/)
may also provide some useful hints. It supports some static typing
syntax, which allows it to safely use more efficient compiled code in
many special cases (without requiring it to use psychic powers in its
static analysis).


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list