[Python-ideas] namedlist() or Record type

Ronald Oussoren ronaldoussoren at mac.com
Wed Jul 24 11:25:42 CEST 2013


On 24 Jul, 2013, at 9:50, anatoly techtonik <techtonik at gmail.com> wrote:

> Rationale: There are many 2D data with table layout where order is
> important and it is hard to work with them in Python.
> 
> The use case:
> 1. Get data row from a table
> 2. Change row column by name
> 3. Save data back
> 
> For example, termios.tcgetattr() returns list [iflag, oflag, cflag,
> lflag, ispeed, ospeed, cc]. I need to modify some bits in lflag value
> and save this list back. In my code it looks like:
> 
>            newattr[3] &= ~termios.ICANON
> 
> I want to get rid of magic number 3 without defining additional
> variables just for names. I thought that namedtuple can help here, but
> it is read-only and seems not serializeable.

Namedtuple has an _update method, which would allow you to
write the code below if tcgetattr returns a named tuple:

     newattr = newattr._replace(cflag=newattr.cflag & ~termios.ICANON)

However, tcgetattr does not return a namedtuple :-)

What do you mean by "namedtuple [...] seems not serializable"? Both
pickle and json work fine with named tuples.

Ronald


More information about the Python-ideas mailing list