What can you do in LISP that you can't do in Python

Alex Martelli aleaxit at yahoo.com
Wed May 23 10:50:59 CEST 2001


"Darren New" <dnew at san.rr.com> wrote in message
news:3B0B4022.4B7A5AD2 at san.rr.com...
> Alex Martelli wrote:
> > Statement, yes -- def is a statement so can't nest 'inside' the use.
> > But *WHY* should the def be outside the function using it?!  Put
> > it right before the use, for Pete's sake... why not?!
>
> Err, so if it's inside a loop, you're running the "def" each time thru
> the loop? Or does Python take care of that for you, and only run the def
> statement once?

The 'def' is run every time, but it does very little -- generate
a new "function object" (just like executing a lambda-form
generates a new "function object" -- function objects can have
a little state [mutable attributes] and one may not want to
carry it over from one leg of the loop to the next one... with
lambda, one does not get a choice) and bind or re-bind the local
variable's 'slot' to it.  The relatively hard work of compilation
is done once in any case, of course -- when the module is first
imported (then, normally, the compiled bytecode is saved to
a .pyc file, etc).


> I'd actually forgotten you can def functions inside other functions.

Ah.  A pretty important little issue, which had led you to
make assertions and implications which those of us who KNEW
"you can def anywhere you like" could (and in my case did)
take as very peculiar indeed.


> Having to name bits of code just so you can pass them to other routines
> is about as annoying (in my experience) as having to declare stuff in
> the order it's used so the compiler can make one pass thru the code.
> Nothing major, but not completely trivial either.

I consider lambdas a minor annoyance, you seem to consider
having to name a function a "not major but not completely
trivial" annoyance, so our stances differ, but by nowhere
as much as when you were claiming that using named functions
instead of lambdas broke OO and SP principles, of course.


> Oh come on. I'm merely comparing python's convenience to smalltalk's
> convenience w.r.t. unnamed functions. The lack of nested scopes and the
> requirement to name every block of code that gets passed around makes it
> more difficult.

Nested scopes are in (though you still need a 'from future
import' in 2.1 -- is that a MAJOR annoyance?-), and a completely
orthogonal issue to naming anyway, so please don't keep throwing
up irrelevancies.

Does Smalltalk support unnamed, on-the-fly-coded *classes*?  As
I recall it doesn't (but I could be wrong), and nor does Python.

You can execute a class statement anywhere, just like you can
execute a def statement anywhere, but in both cases you do get
the 'annoyance' of having to think up a name.  In Python, the
issues wrt function objects and class objects are totally
symmetrical -- pass them around all you like, but do give
them a name upon creation (you can freely rebind that name
at any later time, of course - no problem).  Personally, I like
this symmetry, regularity, and simplicity.  Sure, there might
be a marginal gain of convenience, for some styles of coding,
if the class or def could be embedded right in the middle of
the one and only expression that needs to use the class object,
or function object, but I suspect the complications would not
be worth the gain for such an addition.  Somebody whose entire
"programming lifestyle" is based on passing class objects, or
function objects, around to others, might differ, of course.


> Why not require every statement that needs a block of code to use a
> function too? Then you could get rid of "break" entirely.

No you couldn't, not without losing the ability to code
Knuth's "N times and a half" loop idiom directly.

> I was just disagreeing that the need to give it a name is as
> trivial as you thought it is, that's all.

Actually, you were also making counterfactual claims about
the ability to use a def statement locally, and nested-scope
considerations as well -- partly no doubt because you did
not know/remember correctly the language you were criticizing.

Given that def can be executed where you want, AND that
nested scopes are part of the language (in absolute sync
for lambdas and named-functions: not in 2.0 and earlier,
yes in 2.2 and later, on request in 2.1), it's clear that
the 'having to give a name' IS quite a bit MORE trivial
than YOU thought it was, isn't it?


> Man, you really do have a wild hair, don't you?

I often react hotly to wild, unsupportable and extraordinary
claims, if that is what you mean.  If those claims come in
part from somebody deciding to criticize a language on the
basis of a flawed/incomplete knowledge of that language, I
hardly think this is MY responsibility...:-).


Alex






More information about the Python-list mailing list