[Python-ideas] Re: Generic NamedTuples

2021-02-10 Thread Eric V. Smith

https://bugs.python.org/issue42269

Although I haven't done much about it. The problem is that it needs to 
return a new class, not just inject methods into the existing class you 
defined.


Eric

On 2/10/2021 11:13 AM, Peter Ludemann wrote:

Ben Avrahami  wrote 9 Feb 2021, 03:30:


That's the current alternative, but namedtuples (unlike dataclasses) are 
slotted, which makes them more space-efficient and (slightly) faster.

When I use @dataclass(frozen=True), I manually add a __slots__ attribute. I 
presume this makes the resulting objects similar in size to NamedTuples, but I 
haven't done any measurements (and not sure how to measure this anyway).

I wonder: is it possible for @dataclass to have an option slots=True?
And perhaps frozen=True should imply slots=True (would this cause any backwards 
compatibility problems?).
___
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/OBFNLB363DQM5YELR46M3OI3BNKIKSPB/
Code of Conduct: http://python.org/psf/codeofconduct/

___
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/ZOX4ILCTKO33K56VC7ZW42ONBMCAC6IF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Generic NamedTuples

2021-02-10 Thread Peter Ludemann
Ben Avrahami  wrote 9 Feb 2021, 03:30:

> That's the current alternative, but namedtuples (unlike dataclasses) are 
> slotted, which makes them more space-efficient and (slightly) faster.

When I use @dataclass(frozen=True), I manually add a __slots__ attribute. I 
presume this makes the resulting objects similar in size to NamedTuples, but I 
haven't done any measurements (and not sure how to measure this anyway).

I wonder: is it possible for @dataclass to have an option slots=True? 
And perhaps frozen=True should imply slots=True (would this cause any backwards 
compatibility problems?).
___
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/OBFNLB363DQM5YELR46M3OI3BNKIKSPB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Generic NamedTuples

2021-02-08 Thread Peter Ludemann
I use dataclass (with frozen=True) instead of NamedTuple - and I can do
mixins.
https://docs.python.org/3/library/dataclasses.html
___
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/CGJTA7NHHWJJZZ775RAX3FLC3VRXMEB7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Generic NamedTuples

2021-02-08 Thread Guido van Rossum
Did you bring this up on the original issue?

On Mon, Feb 8, 2021 at 10:21 AM Ben Avrahami  wrote:

> In both python 3.7 and 3.8, typing.NamedTuple had some strange behaviour
> regarding multiple inheritance. This behaviour was solved in 3.9
> (bpo-36517), but this fix also barred me from a feature very close to my
> heart: the generic named tuple
>
> ```
> T = TypeVar('T')
> class LLNode(NamedTuple, Generic[T]):
>   value: T
>   next: Optional[LLNode[T]]
> ```
>
> bpo-36517 was committed because any additional features of other classes
> would be missing from the NamedTuple class at runtime. But Generic has no
> features at runtime, and thus this snippet actually works as expected in
> python 3.7/8. But in 3.9, it raises a TypeError, as bpo-36517 completely
> barred any other bases in a typing.NameTuple subclass.
>
> I propose that instead of wholesale barring multiple inheritance in
> NamedTuple, NamedTuple will allow some pre-defined bases alongside it. It
> will only allow do-nothing mixins with no runtime features (I can only
> think of `Generic` Aliases atm, but perhaps others exist). As before, these
> bases will be actually omitted from the class's bases at runtime.
>
> Any thoughts?
> ___
> 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/XZVZT3U237UCEI4FT6MZVU5MBZICV6ST/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
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/EHHVQ2J5EILIM74BKVQIXE3U4QDLHRBE/
Code of Conduct: http://python.org/psf/codeofconduct/