Why I love python.

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Mon Aug 16 21:38:01 EDT 2004


:> Python needs drastic performance improvement if it is to scrap-off the
:> "scripting language" stigma. The only way to get these improvements is
:> making it possible for a python implementation to produce *efficient*
:> *compiled* code. At the same time the dynamic-typing nature of the
:> language is one of its most valuable characteristics. And this is one
:> of the hardest problems when trying to write a decent python
:> compiler. If you define a function like:
:> 
:>   def sum (a, b):
:>     return a + b
:> 
:> How can the compiler know what code to produce? 

: I know of at least one language which has solved this problem, Ocaml


I'm not quite sure if this is true.  In contrast to Python, a lot of
the type information in OCaml is attached to the operators.  This is
to make the type-inferencing work efficiently.  For example, OCaml's
addition operator is hardcoded to work with integers:

(******)
[dyoo at shoebox dyoo]$ ocaml
        Objective Caml version 3.07+2

# (+) ;;
- : int -> int -> int = <fun>
(******)



and there's a separate operator for adding floats to floats:

(*** OCaml ***)
# ( +. );;
- : float -> float -> float = <fun>
(******)



Python's dynamic lookup of module-level symbols also make things more
difficult than in Ocaml.  In OCaml, names that are bound stay bound:

(*** OCaml ***)
# let x = 42;;
# let say_x () = print_endline (string_of_int x);;
val say_x : unit -> unit = <fun>
# say_x ();;
42
- : unit = ()
# let x = "hello";;
val x : string = "hello"
# say_x ();;
42
- : unit = ()
(******)


Note, again, that the OCaml operators and functions are often
themselves typed to make type-inference work.  This may make things
slightly verbose again.  I could be wrong, but I couldn't find a
simple, generic, "print" function that could print any value in OCaml.


In this example, the say_x function keeps a record of all the name
bindings from before, which is why it remembers the original binding
for 'x'.  This is just fundamentally different from the "late binding"
approach using in Python:

### Python ###
>>> x = 42
>>> def say_x():
...     print x
...
>>> say_x()
42
>>> x = "hello"
>>> say_x()
hello
###


So I'm not so sure that Python's current design makes type-inference
easy.  I'm pretty sure it's a little harder than just yanking out
OCaml's type-inference engine and jury-rigging it into Python.  *grin*



More information about the Python-list mailing list