On Fri, Aug 14, 2020 at 4:38 PM Caleb Donovick <donov...@cs.stanford.edu>
wrote:

> My own personal use for this would be for generating anonymous protocols
> and dataclasses:
>
> class T(Protocol):
>     x: int
>     y: str
> # with some abuse of notation obviously these would generate unique 
> typesassert T == Struct[x=int, y=str]
> # similarly @dataclassclass S:
>    x: int
>    y: str
> assert S == Struct[x=int, y=str]
>
> I often want to create such types “on the fly” without needing to put a
> name on them.
>
> Now as I don’t need mixed keyword / positional arguments I can achieve
> this with:
>
> # K = dict
> Struct[K(x=int, y=str)]
>
> But that costs 3 more keystrokes and is certainly less beautiful.
>
> While I would not personally use this I think a real killer app would be
> slicing named axis, as the slice syntax is exclusive to geitem and hence
> can not leverage the dict trick.
>
To me, the main weakness here is that you couldn't move forward with this
unless you also got the various static type checkers on board. But I don't
think those care much about this use case (an inline notation for what you
can already do with a class definition and annotations). And without static
checking this isn't going to be very popular.

If and when we have `__getitem__` with keyword args we can start thinking
about how to best leverage it in type annotations -- I would assume that
describing axes of objects like numpy arrays would be the first use case.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/3DE7QVHYC4PV2CBAPETROWPJU2YV5HNN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to