With the advent of collections.namedtuple, I thought that having a counterpart in the struct module would make having to deal with unpacked data much easier. Per suggestion, this extends the behavior of _struct.Struct rather than a separate NamedStruct class. The regexp might not be immediately obvious as to what the format required is. The format string is represented like this: "attribute_name1(attribute_format) attribute_name2(attribute_format2)" and so on. Formats given in parentheses without an attribute name are allowed, so that byte order and pad bytes can be specified. For example: "(!) x1(h) x2(h) (2x) y1(h) y2(h)" Suggestions and criticism are welcome. I think it would simplify using the struct module a lot to be able to have named attributes like this.
Okay, here's a new version of my patch. Instead of replacing the default functionality of struct.Struct, this patch now adds the functionality to a separate class called NamedStruct, so as to not break backwards compatibility. The coding style has been revised, and it now also raises a more descriptive error if the regex fails to parse. Also included: a unit test.
Thanks for submitting the patch and the idea. I've previously looked at this approach and decided against it. The struct module is highly optimized and typically called many times in a loop and a patch like this would hurt performance. Also, it adds complexity to the module making it more difficult to learn an remember. It is better to leave named tuples separate and allow users to combine them downstream -- see the SQLite example for instance -- the cast to named tuples could be applied directly, so there was no need for a change to the SQL module. It is better design for us to leave the data retrieval (struct, csv, sql, etc) separate from the code for collections.namedtuple. Instead of this patch, I propose to add a simple example to the struct module documentation showing how to cast to a named tuple. Also, FWIW, the preferred way to cast tuples to named tuples is to use the _make() method instead of using the constructor with the star operator. nt._make(t) instead of nt(*t).