On Thu, Aug 27, 2020 at 1:45 PM David Mertz <me...@gnosis.cx> wrote:

> On Thu, Aug 27, 2020, 1:35 PM Ricky Teachey
>
>> Conceptually, an "immutable collection" serves a different purpose than
>>> "a collection of axes", even if they work then same under the hood.
>>>
>>
>> What about something like this:
>>
>> class Name(NamedTuple):
>>     first: str
>>     last: str
>>
>> d = NamedKeyDict(Named)
>> d[first='david', last='mertz'] = 1_000_000  # dollars
>>
>
> Sure, maybe. But this is probably better as a dataclass nowadays.
> Actually, I'm not sure what NamedKeyDict is meant to do in your example.
>

Just intended to be a dictionary that uses a named tuple type (or other
type with the same named attributes available) as the keys. You initialize
it by telling it what the type is, similar to defaultdict:

dd = defaultdict(list)

...except the type is a NT-like class:

class MyNamedTuple(NamedTuple):
    spam: str
    eggs: str

nd =  NamedKeyDict(MyNamedTuple)

Now if you add a key using named arguments, it will call MyNamedTuple and
supply them as kwargs:

nd[spam="foo", eggs="bar"] = "baz"

...results in:

>>> nd
{MyNamedTuple(spam='foo', eggs='bar'): 'baz'}


---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JL5I2WH5S4VLJ3BX5MHFC4QIG7FPCB7O/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to