Python vs. Lisp -- please explain

6

63q2o4i02

Hi, I've been thinking about Python vs. Lisp. I've been learning
Python the past few months and like it very much. A few years ago I
had an AI class where we had to use Lisp, and I absolutely hated it,
having learned C++ a few years prior. They didn't teach Lisp at all
and instead expected us to learn on our own. I wasn't aware I had to
uproot my thought process to "get" it and wound up feeling like a
moron.

In learning Python I've read more about Lisp than when I was actually
trying to learn it, and it seems that the two languages have lots of
similarities:

http://www.norvig.com/python-lisp.html

I'm wondering if someone can explain to me please what it is about
Python that is so different from Lisp that it can't be compiled into
something as fast as compiled Lisp? From this above website and
others, I've learned that compiled Lisp can be nearly as fast as C/C++,
so I don't understand why Python can't also eventually be as efficient?
Is there some *specific* basic reason it's tough? Or is it that this
type of problem in general is tough, and Lisp has 40+ years vs Python's
~15 years?


Thanks
Michael
 
D

DH

A few years ago I
had an AI class where we had to use Lisp, and I absolutely hated it,
having learned C++ a few years prior. They didn't teach Lisp at all
and instead expected us to learn on our own.

CS classes haven't changed, I see.
In learning Python I've read more about Lisp than when I was actually
trying to learn it, and it seems that the two languages have lots of
similarities:

http://www.norvig.com/python-lisp.html

I'm wondering if someone can explain to me please what it is about
Python that is so different from Lisp that it can't be compiled into
something as fast as compiled Lisp? From this above website and
others, I've learned that compiled Lisp can be nearly as fast as C/C++,
so I don't understand why Python can't also eventually be as efficient?
Is there some *specific* basic reason it's tough? Or is it that this
type of problem in general is tough, and Lisp has 40+ years vs Python's
~15 years?

It is by design. Python is dynamically typed. It is essentially an
interpreted scripting language like javascript or ruby or perl, although
python fans will be quick to tell you python is compiled to byte code.
They'll also be quick to tell you:
-python has true closures (although nothing like ruby's blocks)
-is beginner friendly (despite being case sensitive and 3/4==0, for example)
-is not, in fact, slow at all (despite benchmarks as you noted showing
otherwise).
Judge for yourself.

There are projects that combine static typing + the python syntax, which
result in dramatically faster code, but perhaps only 80% of python's
functionality and less flexibility you get from dynamic typing.
Projects like shedskin. But some python fans don't think 80% cuts it,
even if you do get a 100 fold speed increase.
 
T

Terry Reedy

In learning Python I've read more about Lisp than when I was actually
trying to learn it, and it seems that the two languages have lots of
similarities:

http://www.norvig.com/python-lisp.html

I'm wondering if someone can explain to me please what it is about
Python that is so different from Lisp that it can't be compiled into
something as fast as compiled Lisp? From this above website and
others, I've learned that compiled Lisp can be nearly as fast as C/C++,

In order to be that fast, some of the dynamism of intepreted Lisp must be
given up. In particular object code is not list data. Python with
type-dynamism eliminated can also be translated to decent C/C++ and then
compiled. See PyRex and Weave. There is also Psyco, which I believe
translates directly to machine code.
so I don't understand why Python can't also eventually be as efficient?
Is there some *specific* basic reason it's tough? Or is it that this
type of problem in general is tough, and Lisp has 40+ years vs Python's
~15 years?

Yes, *much* more work has gone into Lisp than Python. (At least 10x, I am
sure. and maybe up to 100x) During the 1980s, there was a Lisp/AI
boom/bust something like the dot.com boom/bust of the last 1990s with
perhaps a billion invested in Lisp/AI companies. I presume some of that
went into Lisp itself (as opposed to AI applications thereof).

Terry Jan Reedy
 
G

gene tani

I'm wondering if someone can explain to me please what it is about
Python that is so different from Lisp that it can't be compiled into
something as fast as compiled Lisp? From this above website and
others, I've learned that compiled Lisp can be nearly as fast as C/C++,
so I don't understand why Python can't also eventually be as efficient?
Is there some *specific* basic reason it's tough? Or is it that this
type of problem in general is tough, and Lisp has 40+ years vs Python's
~15 years?

i'm not sure it'll answer question, but Brett cannon's thesis is good
background

http://www.ocf.berkeley.edu/~bac/thesis.pdf
 
S

Steven D'Aprano

It is by design.

You make it sound like Guido sat down to design a language and
deliberately put "Slow" first on his list of desired attributes. Why such
a negative tone to your post?

Python is not slow by design. Python is dynamically typed by design, and
relative slowness is the trade-off that has to be made to give dynamic
types.

The Python developers have also done marvels at speeding up Python since
the early days, with the ultimate aim of the PyPy project to make Python
as fast as C, if not faster. In the meantime, the question people should
be asking isn't "Is Python fast?" but "Is Python fast enough?".
Python is dynamically typed. It is essentially an
interpreted scripting language like javascript or ruby or perl, although
python fans will be quick to tell you python is compiled to byte code.

You make it sound like Python fans are bending the truth. That Python
compiles to byte-code is an objective fact which can be learnt by anyone,
not just "Python fans". In that regard, Python is closer to Java than Perl
or Javascript. Only without the slow startup time of the JRE.

They'll also be quick to tell you:
-python has true closures (although nothing like ruby's blocks)

They're also nothing like Pascal's with statements either. Why make the
comparison with Ruby's blocks when the original poster is comparing Python
to Lisp?

-is beginner friendly (despite being case sensitive and 3/4==0, for
example)

Case sensitivity isn't beginner unfriendly. It is sloppy thinker
unfriendly. Whether you have been programming for thirty days or thirty
years, if you don't know the difference between foo and FOO you have a
serious problem. As they say, case is the difference between "I helped my
Uncle Jack off a horse" and "I helped my uncle jack off a horse."

As for the difference between integer division and true division, yes,
that was an unfortunate design decision. Fortunately it is being rectified
in the least painful way possible.

-is not, in fact, slow at all (despite benchmarks as you noted showing
otherwise).

I've asked this question before, but obviously there is a particular
mindset that just doesn't get it. Slow compared to what? Slow to do what?

For those who don't understand the difference between "faster" and "fast
enough", perhaps a simple analogy will bring enlightenment. For most
people, under most circumstances, an ordinary car (top speed around 75
mph or so) is fast enough, more convenient, and much better value for
money than the significantly faster F-15 fighter plane (top speed around
1850 mph).

On modern hardware, for the vast majority of applications, the execution
speed of the language is not the limiting factor. I/O or the algorithm is
usually the limiting factor. "Change your algorithm" is often better
advice than "change your language".

That's not to say that Python isn't objectively SLOWER (note the
relative term, not absolute) than some languages. But execution speed is
rarely the most important factor in choice of a language.

For those tasks that language speed is a limiting factor (say, writing
devise drivers, operating systems, and similar), Python may not be fast
enough. But they are the exception rather than the rule, and there are no
shortage of ways around that: Psycho, C extensions, or even "choose
another language".
 
R

Robert J. Hansen

what [is it] about Python that is so different from Lisp that it
can't be compiled into something as fast as compiled Lisp?

IMO, it's the lack of competing implementations.

LISP has a very well-defined ANSI specification. Lots of different
people have written LISPs, from Franz to Steel Bank to GNU to... etc.
Each of these competes with the others on different grounds; some are
purely interpreted (ala CLISP), some are compiled (CMUCL), some are...
etc. They all implement substantially the same language, but the
plethora of different implementations has been a tremendous boon for
the development of efficient compilers, interpreters and garbage
collectors.

By comparison, Python lacks anywhere near as many competing
implementations.

In some respects the lack of competing implementations is a Good Thing.
In some respects it's a Bad Thing. On balance it's just a thing.
 
B

bearophileHUGS

The question about the speed is interesting. Generally the more dynamic
and flexible your language is, the slower are the programs it produces.
Lisp is almost unique in such matters (and in this regard it's maybe
better than CPython) because it allows the programmer to mix and blend
different levels of how much dynamism and how much optimized a part of
the program has to be. When you want to speed up a part of a Lisp
program (often developed starting from a slow but flexible code) you
start to use more specific functions, unboxed variables, simple arrays
of unboxed data, statically typed variables, etc. It's not easy, but if
you have some experience, often than one year, in that way you can
write programs only 20% or 50% slower than ones written in C, so for
most purposes they become fast enough. On the other hand you can
develop the program using an interactive shell and a dynamically typed
language, that speeds you a lot the prototyping, etc. You can have the
best of both things.
Python allows to mix various blends too (Psyco, Pyrex, ShedSkin, Weave,
SWIG, glues for Fortran and C/C++, etc), but in Lisp such blending
seems much more natural, finer grained, integrated in the language.
I think this isn't a limit of the language itself, because with enough
work two tools like Psyco and ShedSkin can become integrated in the
language itself (I think Psyco and SS are enough for 98% or purposes)
producing something not far from the "optimization flexibility" of
Lisp. Psyco compiles just in time some code, ShedSkin is more
aggressive, but if it becomes developed enough it can produce programs
fast as C/C++ ones with the same syntax of Python (some things aren't
supported). So then you can develop a program like in Lisp, where some
parts aren't optimized, some parts are compiled just in time, and other
critical parts are type inferenced and statically compiled for the max
speed (this requires to join ShedSkin with something like Swig, to
allow a natural, fully automatic and pythonic joining of .PYDs produced
by SS and CPython programs). PyPy can probably solve such problems in
similar ways or better, we'll see.

Bye,
bearophile
 
R

Roy Smith

DH said:
-python has true closures (although nothing like ruby's blocks)

What is a "true closure"? Or, maybe what I'm asking is what kind of
closure wouldn't be a true closure? Is there some kind of ersatz closure
other language try to pass off, in violation of truth in closure laws?
 
R

Roy Smith

Robert J. Hansen said:
LISP has a very well-defined ANSI specification. Lots of different
people have written LISPs, from Franz to Steel Bank to GNU to... etc.
Each of these competes with the others on different grounds; some are
purely interpreted (ala CLISP), some are compiled (CMUCL), some are...
etc. They all implement substantially the same language, but the
plethora of different implementations has been a tremendous boon for
the development of efficient compilers, interpreters and garbage
collectors.

It's been a while since I've dabbled in lisp, but my recollection is that
the plethora of different implementations has also meant that portability
is a fantasy.

I dread the day that competing Python implementations spring up.
 
T

Torsten Bronger

Hallöchen!

Roy Smith said:
[...]

It's been a while since I've dabbled in lisp, but my recollection
is that the plethora of different implementations has also meant
that portability is a fantasy.

I dread the day that competing Python implementations spring up.

Even worse: In one of them Microsoft is involved.

Tschö,
Torsten.
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

IMO, it's the lack of competing implementations.

I beg to differ in this point.
There are other implementations, but they are not called "python" and
they are not a 100% python in syntax and features.
For example, Boo is 95% python syntax wise, but statically typed.
This fundamental difference makes it as fast as C# or any other .NET
(or mono) language.
Being statically typed doesn't mean that you have to declare types
everywhere, like in C. It uses type inference, so you can declare a
variable x= 5 and the compiler will know that x is an integer of value
5.
Pyrex is statically typed too, but it's used mainly as an extension
language for Cpython.

Now talking specifically about python, there are projects aimed at
speeding it up substantially:

Pypy is a project that relies heavily in type inference (for
translation to lower level code) and dynamic optimization. It's based
mainly on psyco, which has already probed that it can massively speed
up python code.

Shed-Skin: it's a pyton-to-c++ compiler. It won't support the most
dynamic features of python, and requires the programmer to restric a
little bit his coding style in order to allow static compilation, but
so far it looks great (only one developer though..).
 
B

bearophileHUGS

Luis M. González>[Shed-Skin] ... but so far it looks great (only one
developer though..).<

Two developers, I am there too :)

I think people aren't much interested so far because there aren't good
ways to link/join/use SSPython compied code from CPython code. A good
solution is probably to:
- Develop code in CPython
- find if there are slow spots that Psyco can't improve enough
- in this situation move the parts in a module and do some tweaks to
adapt the code (if necessary)
- compile the .py module (to a .pyd, etc) with SS (a single click can
be enough on some operating systems), and then import it as before.

To do this it SS requires to know the types of the input data of the
functions/classes in the module. There are solutions to this, the
simpler one to me seems to infer such types from the code below the if
__name__ == "__main__":
That part can contain tests of all the functions/classes, so such parts
can be used to infer types of the whole module.
To do this automatically SS has to use something like SWIG.

Other developers can probably help with such things, otherwise SS will
probably be dead in a year from now... this is not good for me.

Bye,
bearophile
 
A

Alexander Schmolck

Terry Reedy said:
In order to be that fast, some of the dynamism of intepreted Lisp must be
given up. In particular object code is not list data.

I'm not entirely sure what you are talking about, but you're almost certainly
very confused about something. Not all common lisp implementations even have
an interpreter (which doesn't tend to be visible to the user).

'as
 
C

Cameron Laird

What is a "true closure"? Or, maybe what I'm asking is what kind of
closure wouldn't be a true closure? Is there some kind of ersatz closure
other language try to pass off, in violation of truth in closure laws?

It's an apt question.

What's a "true closure"? That's the easiest part, in some ways:
Wikipedia, for example, tells that it's "a function that refers
to free variables in its lexical context" <URL:
http://en.wikipedia.org/wiki/Closure_(computer_science) >,
that is the context of the domain of the function's definition.

Some languages--Lisp, but also many others--definitely have it.
Basic definitely didn't. Tcl ... well, after weeks of discussion
<URL: http://wiki.tcl.tk/closures >, the conclusion was that Tcl
almost has as much as it can, given that "lexical context" just
doesn't have much standing in Tclonia. So, yes, George Mikan and
Allen Iverson both definitely played basketball, and well, but,
at the same time, it's useful to distinguish the things they do
with a ball.
 
K

Kay Schluehr

Roy said:
What is a "true closure"? Or, maybe what I'm asking is what kind of
closure wouldn't be a true closure? Is there some kind of ersatz closure
other language try to pass off, in violation of truth in closure laws?

A "true closure" is what Python doesn't have ;)

If you enclose variables in a certain functional context in which they
are not defined they are turned into something immutable in Python.
Assigning a value to the same name creates a new object instead of
rebinding the old name. This "readonly" semantics confuses many
programmers coming from other languages at least all Lispers/Schemers
I've talked to. Python does not provide a rebinding operator for free
variables by BDFL decision.

Kay
 
T

Terry Hancock

In order to be that fast, some of the dynamism of
intepreted Lisp must be given up. In particular object
code is not list data. Python with type-dynamism
eliminated can also be translated to decent C/C++ and then
compiled. See PyRex and Weave. There is also Psyco,
which I believe translates directly to machine code.

I thought it was just "Pyrex" as in "Still as clear as
glass, but can really take the heat.". ;-)

Now it's a small snake / dog chimera. Eeeww. You've ruined
it for me.

Otherwise, I think this has been well-answered -- if you
give up the same features, you can get the same speed. But
who cares?

Those things only matter in a very limited domain, and real
programs can use Python for logic and Python extension
modules for things that truly need optimization. If you use
Pyrex, you can even still pretend you're programming in
Python when you write those extensions. I'm sure that's why
some 3D libraries have opted to write the fast code in Pyrex
instead of C (even though either is possible).

--
Terry Hancock ([email protected])
Anansi Spaceworks http://www.AnansiSpaceworks.com
 
A

Alexander Schmolck

Hi, I've been thinking about Python vs. Lisp. I've been learning
Python the past few months and like it very much. A few years ago I
had an AI class where we had to use Lisp, and I absolutely hated it,
having learned C++ a few years prior. They didn't teach Lisp at all
and instead expected us to learn on our own. I wasn't aware I had to
uproot my thought process to "get" it and wound up feeling like a
moron.

In learning Python I've read more about Lisp than when I was actually
trying to learn it, and it seems that the two languages have lots of
similarities:

http://www.norvig.com/python-lisp.html

I'm wondering if someone can explain to me please what it is about
Python that is so different from Lisp that it can't be compiled into
something as fast as compiled Lisp?

Nothing. Given a sufficiently smart implementation any language can be as fast
as any other -- it might just be a billion times harder to write that
implementation for language A than for language B.
From this above website and others, I've learned that compiled Lisp can be
nearly as fast as C/C++, so I don't understand why Python can't also
eventually be as efficient? Is there some *specific* basic reason it's
tough? Or is it that this type of problem in general is tough, and Lisp has
40+ years vs Python's ~15 years?

I think if you're looking for one single reason, it is presumably that (IIRC)
python was designed on the erronous assumption that dynamically typed
languages have to be slow (and are unsuitable for real applications anyway)
wheras common lisp wasn't. Furthermore the people involved in common lisp were
much more knowledgeable and experienced in things like compiler design and had
a long history of similar languages and various implementations to build upon.

As common lisp and scheme demonstrate you can have high level of dynamism (and
in a number of things both are more dynamic than python) and still get very
good performance (in some cases close to or better than C). But both these
languages have been designed with compiler writers and the generation of fast
code in mind, so they made design decisions to ease writing fast lisp
compilers and programs.

For example:

- python classes (and to some extent modules) are essentially dictionaries
that you can modify and customize more or less at will at run-time and that
behave interchangeably in many respects. I'm sure that presents several
optimization headaches.

By contrast if the common lisp compiler sees the symbol CL:LIST (usually
written just LIST, because the CL package is imported by default) it can
safely assume that it refers to the builtin LIST function, because you're
not allowed to rebind the function value of functions in the CL package.
Python can assume no such thing if it comes across ``list`` -- for all it
knows it might as well be the number 42. Also the package and class system
are completely separate and although common lisp's OO system is rather more
powerful than python's it has been designed to be implementable efficiently.

- in python almost everything has to happen at run-time, whereas in common
lisp you can do things at compile time, load time or run-time e.g:

- common lisp has a mechanism for making compiler declarations (so you can
tell the compiler to inline a function, or the type of a variable, or to
optimize something for speed and not for space etc.)

- common lisp has macros (proper ones, not C style) which allow you to build
efficient abstractions

- common lisp has compiler macros. This sort of means that you can write your
own compiler optimizations for your functions (i.e. if you know that your
expensive FOO function is indempotent you could arrange for all calls of the
form (FOO (FOO A)) to be replaced with simply A, in a similar way as an
optimizing compiler might replace (a+b+c+d)*0 with 0).

What's far more interesting to me, however, is that I think there a good
reasons to suspect python's slowness is more of a feature than a flaw: I'd not
be suprised if on the whole it greatly increases programmer productivity and
results in clearer and more uniform code.

If you know the language to be dog slow any way, you're much less likely to
waste your time (and that of future maintainers) on the pointless
microoptimizations that geeks so love. Also, since only builtins have
reasonable performance there's added motiviation to become very familiar with
the available builtins (and standard libarary) and far less temptation to roll
one's own version of say dict.setdefault (even if it it sucks). The fact that
non-standard library code is inherently somewhat inferior (because it will
either be written in python and slow or written in C and a pain to install)
adds further incentive to attempt community wide standardization.

I think it's not unreasonable to speculate that all this decreases production,
maintenance and reuse costs of python code considerably, so much in fact that
python's very slowness represents part of its competetive edge over languages
that are in some ways better engineered and more capable.

So ironically, some share of python's success might actually be due to
ignorance on Guido's part (of course python in most respects is a marvellously
well designed language that to my mind places Guido at least on par with the
designers of any contemporary language; I'm just intrigued by the possiblity
that had he known as much about performance issues in very dynamic languages
as say some of the lisp and self people, python might have turned out to be a
faster albeit less popular and productive language).

'as
 
A

Alexander Schmolck

Bruno Desthuilliers said:
DH a écrit :
(snip)


It's not a "scripting" language, and it's not interpreted.

Of course it is. What do you think happens to the bytecode? And if python
isn't a scripting language, then what on earth is?

You might want to argue about whether scriping language is a meaningful and
useful concept, but it's really hard to see how you could talk about "scripting
languages" without including python.

'as
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top