This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: struct.Struct.unpack to return a namedtuple for easier attribute access
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: habnabit, martin.panter, rhettinger
Priority: normal Keywords: patch

Created on 2008-05-18 20:13 by habnabit, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
named-struct.patch habnabit, 2008-05-18 20:13 Patch against the py3k branch
named-struct2.patch habnabit, 2008-05-18 21:41 Patch against the python trunk
Messages (4)
msg67038 - (view) Author: Aaron Gallagher (habnabit) Date: 2008-05-18 20:13
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.
msg67048 - (view) Author: Aaron Gallagher (habnabit) Date: 2008-05-18 21:41
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 
msg67050 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-05-18 22:03
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 

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).
msg67253 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-05-23 17:22
Added example to docs.  See r63564.
Date User Action Args
2022-04-11 14:56:34adminsetgithub: 47158
2012-10-27 04:17:52martin.pantersetnosy: + martin.panter
2008-05-23 17:22:36rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg67253
2008-05-18 22:03:25rhettingersetmessages: + msg67050
2008-05-18 21:41:13habnabitsetfiles: + named-struct2.patch
messages: + msg67048
versions: + Python 2.6
2008-05-18 21:19:16rhettingersetassignee: rhettinger
type: enhancement
nosy: + rhettinger
2008-05-18 20:13:19habnabitcreate