Structures
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Nov 3 22:26:06 EST 2008
On Tue, 04 Nov 2008 00:19:16 +0000, Marc 'BlackJack' Rintsch wrote:
> On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
>
>> What's then the reason for adding named tuples if they are not
>> mutable...???
>
> Names are more descriptive than "magic numbers" as indices. See for
> example the "named tuple" returned by `os.stat()`.
I have no objection to named tuples, but I've sometimes missed having an
equivalent to the Pascal record or C struct: essentially a named mutable
tuple. Here's a quick way to get one:
def record(**kwargs):
"""Lightweight named mutable tuple equivalent."""
class Record(object):
__slots__ = kwargs.keys()
x = Record()
for key, value in kwargs.items():
setattr(x, key, value)
return x
It needs more work to be a full-fledged record, e.g. __eq__ and __str__,
but it's a start.
--
Steven
More information about the Python-list
mailing list