[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake
Bruce Leban
bruce at leapyear.org
Wed Oct 8 17:47:23 CEST 2008
On Wed, Oct 8, 2008 at 6:08 AM, Jan Kanis <jan.kanis at phil.uu.nl> wrote:
> On 06/10/2008, Arnaud Delobelle <arnodel at googlemail.com> wrote:
> > for a[0] in range(10):
> > lst.append(lambda: a[0])
> > for f in lst:
> > print(f())
>
> But I think this would be a better solution:
> 2) Treat location specifiers other than local and global variables
> (variables you can write down without using dots or square brackets)
> the same as they are treated today. In that case, both loops would
> print ten times 9. I would want to argue this is the better approach,
> because when you write down a[0] you are specifically thinking of a
> and a[0] in terms of objects, while when you use a local variable you
> just need some place to store a temporary result, and not be bothered
> with it any more than absolutely necessary.
I don't think this is better. Not that I'm proposing we add macros to the
language but if we did then
macro find(a, x, i):
for i in range(len(x)):
if x[i]:
return lambda: x[i]
would operate differently for find(a, x, i) and find(a, x, i[0]). I think
that's a bad idea.
--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081008/960d70ec/attachment.html>
More information about the Python-ideas
mailing list