I think I have to agree with Petr. Define explicit type names.

On Tue, Jul 30, 2019 at 2:45 AM Paul Moore <p.f.mo...@gmail.com> wrote:

> On Tue, 30 Jul 2019 at 09:33, Christian Tismer <tis...@stackless.com>
> wrote:
> > >>> typing.NamedTuple("__f", x=int, y=int)
> > <class '__main__.__f'>
> > >>> typing.NamedTuple("__f", x=int, y=int) is typing.NamedTuple("__f",
> > x=int, y=int)
> > False
>
> This appears to go right back to collections.namedtuple:
>
> >>> from collections import namedtuple
> >>> n1 = namedtuple('f', ['a', 'b', 'c'])
> >>> n2 = namedtuple('f', ['a', 'b', 'c'])
> >>> n1 is n2
> False
>
> I found that surprising, as I expected the named tuple type to be
> cached based on the declared name 'f'. But it's been that way forever
> so obviously my intuition here is wrong. But maybe it would be useful
> for this case if there *was* a way to base named tuple identity off
> the name/fields? It could be as simple as caching the results:
>
> >>> from functools import lru_cache
> >>> cached_namedtuple = lru_cache(None)(namedtuple)
> >>> n1 = cached_namedtuple('f', ('a', 'b', 'c')) # A tuple rather than a
> list of field names, as lists aren't hashable
> >>> n2 = cached_namedtuple('f', ('a', 'b', 'c'))
> >>> n1 is n2
> True
>
> Paul
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him/his **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/GIFRTFWPEGKZ33PTW63YXKGXHHAQJ35I/

Reply via email to