[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake
Terry Reedy
tjreedy at udel.edu
Sat Oct 4 05:08:28 CEST 2008
Greg Ewing wrote:
> Terry Reedy wrote:
>
>> Here are 5 more alternatives that have the same effect:
>
> All of which are even worse, to my eyes.
If you mean worse than the simple default arg hack, I could be
persuaded. But the default arg issue is a red herring. There is no need
for what you call abuse, as I amply demonstrated, and there is no way
for you to stop others from (mis)using them without removing them.
If you mean worse than turning for-loops into an esoteric CS monster (my
view), we disagree.
My deeper objection is this. Your intended-to-be-motivating example,
similar to what others have occasionally posted, is a toy snippet that
illustrates some points of Python behavior, but which I see no use for
in real application code. Given def f(i): return i; your lst[i]() is
equivalent to f(i). So just write and use the function.
OK, to be persnickety, we need more code, of about the same length as
needed to generate lst:
def f(i):
if not isinstance(i,int): raise TypeError("requires int i")
if not -10 <=i <10: raise ValueError("requires -10 <= i < 10")
return i
So, as near as I can see, your list of identical functions with variant
closure cells simulates type and range checking. What is the point of that?
Perhaps you have an undisclosed real use case with much more complicated
closures. It would still be true that the array index could instead to
fed to the function as an arg. If there were really a reason not to do
that, the 15 keystroke overhead would be relatively much smaller for
more complicated (and realistic) closures.
Terry Jan Reedy
More information about the Python-ideas
mailing list