[New-bugs-announce] [issue13094] setattr misbehaves when used with lambdas inside for loop
report at bugs.python.org
Mon Oct 3 16:39:57 CEST 2011
New submission from Tomáš Dvořák <dvtomas at gmail.com>:
I have this python script, and run it in python 2.7.2 (installed from EPD_free 7.1-2 (32-bit), but I guess this has nothing to do with EPD.
x = X()
items = ["foo", "bar", "baz"]
for each in items:
setattr(x, each, lambda: each)
I'd naively expect it to print
,but it surprisingly (and annoyingly) outputs
Please, tell me that this is a bug :) I'd hate it if this was the intended behaviour. I spent two hours today before I found out this was the cause of my program to fail.
title: setattr misbehaves when used with lambdas inside for loop
versions: Python 2.7
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce