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

Stephen J. Turnbull stephen at xemacs.org
Wed Mar 11 04:43:29 CET 2009

Terry Reedy writes:
 > Stephen J. Turnbull wrote:
 > > Terry Reedy writes:

 > >  > Like many other Pythonistas I recognize that that an
 > >  > uninformative stock name of '<lambda>' is defective relative
 > >  > to an informative name that points back to readable code.
 > >  > What I dislike is the anonymity-cult claim that the defect is
 > >  > a virtue.
 > > 
 > > That's unfair.
 > It is unfair to dislike false statements?

No.  It is unfair to use factives: the proponents of code blocks don't
claim that the uninformative name is a virtue.  They claim that the
effort required to deal with an unnecessary name is a defect, and so
removing that effort is a virtue.  You evidently have no answer for
that argument, so you reinterpret in precisely the kind of word-
twisting way that bothers you when I do it:

 > > Python has "anonymous blocks" all over the place, since every
 > > control structure controls one or more of them.  It simply
 > > requires that they be forgotten at the next DEDENT.  Surely you
 > > don't advocate that each of them should get a name!
 > Surely, I did not.  And surely you cannot really think I suggested such.

That was a rhetorical question, properly marked as such with a
exclamation point rather than a question mark.  Not to mention being
immediately preceded by the obviously correct rationale for the
obviously correct answer.  How did you miss it?

I did have a real point, which was that if the only use of an
anonymous block is immediately juxtaposed to that DEDENT, is there
really any harm to the lack of the name?  In fact, in debugging you
have the name of the using function (which should be short and
readable, or you're not going to have fun anyway), and a line number,
so there is no trouble identifying the problematic code, nor the
execution history that led to it.  A good debugger might even provide
the arguments to the code block as part of the stack trace, which you
would have to go to extra effort to get if it were presented merely as
a suite. Ie, in this kind of use case a code block could be considered
a kind of meta-syntax that tells debuggers "these variables are of
interest, so present me, and them, as a pseudo-stack frame".

The sticking point, AIUI, is that the code block proponents have not
identified a use case where the anonymous block is immediately used,
while there is no Pythonic equivalent.  So that "harmless" (YMMV)
extension is unnecessary, and the extension violates TOOWTDI if that's
all it's good for.  The real power of "code blocks" (that Python
doesn't have) comes when they can be passed around as objects ... but
there doesn't seem to be a way to define them so as to exclude the
obnoxious uses such as in callbacks, or indeed such a use case other
than callbacks.  And that violates the Pythonista's sense of good

So what I want to know (and my question is directed to the code block
proponents, not to you) is where is the Pythonic use case?  All those
Ruby programmers can't be wrong ... can they?  Of course they can!<wink>
But even so, I'd like to understand what the code block proponents
think they're seeing that isn't there, at least not for you and me.
*We* (including the BDFL!?) could be wrong, too, maybe there is
something special about code blocks that Python could benefit from
incorporating.  Or maybe there's a better way to teach Python, so that
people will use Pythonic idioms instead of reaching for "code blocks."

More information about the Python-ideas mailing list