Namedtuples problem

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Feb 24 00:07:07 EST 2017


Deborah Swanson wrote:
> I've run into this kind of problem with namedtuples before, trying to
> access field values with variable names, like:
> 
> label = 'Location'
> records.label

If you need to access an attribute whose name is computed
at run time, you want getattr() and setattr():

    value = getattr(record, label)

    setattr(record, label, value)

Using these, I suspect you'll be able to eliminate all
the stuff you've got that messes around with indexes to
access field values, making the code a lot simpler and
easier to follow.

-- 
Greg



More information about the Python-list mailing list