[Python-ideas] Anonymous namedtuples
random832 at fastmail.com
Tue Apr 19 10:26:41 EDT 2016
On Tue, Apr 19, 2016, at 06:25, Paul Moore wrote:
> Possibly the docs for namedtuple should refer the user to
> SimpleNamespace as an alternative if "being a tuple subclass" isn't an
> important requirement.
Just my two cents - for the use cases that I would find this useful for
(I usually end up just using tuple), a hashable "FrozenSimpleNamespace"
would be nice. Or even one that has immutable key fields that are used
as the basis for the hash but that you can hang other values off of
A way to simply initialize a SimpleNamespace-like class with a passed-in
mapping, and have it inherit the equality/hash/etc from that (so
FrozenSimpleNamespace could use FrozenDict, you could do one with
OrderedDict, one with chainmap or with an easy-to-write custom dict that
provides the "immutable key plus mutable other" behavior), might provide
for all of these in a relatively easy way. Maybe have
SimpleNamespace([dict], /, **kwargs) that just throws the dict argument
(if present) into __dict__.
If there were a FrozenOrderedDict you could even implement a very
namedtuple-like class that way.
More information about the Python-ideas