Namedtuples: some unexpected inconveniences
__peter__ at web.de
Wed Apr 12 18:15:18 EDT 2017
Deborah Swanson wrote:
>> >value = getattr(record, label)
>> That should work.
> We may agree that it *should* work, by an intuitive grasp of how it
> should work, but it doesn't. You get "object has no attribute 'label'.
Only if the namedtuple
(1) does not have a label attribute and
(2) the value of the name label is the string "label"
In that case both
label = "label"
will fail with the same AttributeError. The problem is *not* the dynamic
access through getattr().
>> Indeed you cannot change the namedtuple's attributes. Like the "normal"
>> tuple it is designed to be immutable. If you want changes in one list
>> (the group) to appear in another (the original records) you need a
>> mutable data type.
> Sadly, that does seem to be the correct conclusion here.
Think hard if you really need the original list.
More information about the Python-list