[New-bugs-announce] [issue20230] structseq types should expose _fields
report at bugs.python.org
Mon Jan 13 02:12:54 CET 2014
New submission from Andrew Barnert:
A PyStructSequence type doesn't expose its field names in any way to Python code (except indirectly, via the repr, which you could parse if you really wanted to…).
The docs claim that "Struct sequence objects are the C equivalent of namedtuple() objects", so it seems like the obvious way to expose them is the exact same way namedtuple types do—as a class attribute named "_fields".
Also, in #7796, and in a recent thread on python-ideas, multiple people suggested that "named tuple" is a protocol, not a type, and duck typing on _fields is the obvious way to detect types matching that protocol.
Only the "sequence fields" need to be exposed—fields that don't match up to sequence indices aren't named tuple fields, they're just extra instance attributes (that don't even show up in the repr).
Patch included. For the tests, I tested the fields in struct_time and stat_result in effectively the same way test_repr tests their repr, so it should pass on all the same platforms/builds.
title: structseq types should expose _fields
Added file: http://bugs.python.org/file33436/structseq.diff
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce