[Python-ideas] Ruby-style Blocks in Python Idea

Stephen J. Turnbull stephen at xemacs.org
Thu Mar 12 02:55:52 CET 2009


Guido van Rossum writes:

 > > (add-hook 'text-mode-hook (defun turn-on-auto-fill () (auto-fill-mode 1)))
 > >
 > > neatly avoids the problem by returning the name of the function, the
 > > symbol `turn-on-auto-fill', which is callable and so suitable for
 > > hanging on the hook.
 > 
 > Got it -- sort of like using assignment in an expression in C, to set
 > a variable and return the valule (but not quite, don't worry :-).

Yes.  And no, I don't worry about you, but I do worry about what else
may be lurking in a language whose designer(s) chose to return the
function definition (rather than the name) from define-function. ;-)

 > I assume you're talking about Andrew Koenig's use case -- ANK is
 > Andrew Kuchling, who AFAIK didn't participate in this thread. :-)

Oops, my bad.  Very sorry to all concerned.

 > IIUC (my Lisp is very rusty) this just assigns unique names to the
 > functions right?

Yes.

 > You're saying this to satisfy the people who insist that __name__
 > is always useful right? But it seems to be marginally useful here
 > since the names don't occur in the source. (?)

But they are at least cosmetically useful to the runtime system (eg,
they will be used in reporting tracebacks -- bytecode in the backtrace
is hard to read) and accessible to the user (for redefining a callback
on-the-fly).  The user can't necessarily access the array of callbacks
directly (eg, it might be in C) or conveniently (it may be buried deep
in a complex structure).  It seems plausible to me that the user is
most likely to want to redefine a callback that just blew up, too, and
this would give you the necessary "handle" in the backtrace.

Also, I haven't thought this through, but use of numbers to
differentiate the names was just an easy example.  An appropriate
naming scheme might make it easy to find a skeleton in the source for
the generated callback.  Eg, if instead of numbers the identifiers
were "foo-abort", "foo-retry", and "foo-fail".  I don't know if that
would be useful in Andrew's use-case.

So, yes, marginal, in the sense that I doubt the use cases are common,
but I suspect in a few it could be a great convenience.  How useful in
Python, I don't know ... Emacs Lisp is full of "seemed like the thing
to do at the time" design, so the more handles I have the happier I am.




More information about the Python-ideas mailing list