[Python-Dev] Re: typing: how to use names in result-tuples?

2019年7月30日 01:32:14 -0700

Yes, maybe I can use that.
With Python >= 3.6, also
def f() -> typing.NamedTuple("__f", x=int, y=int): ...
which is concise, but a slightly weird abuse.
But there are other drawbacks:
>>> typing.Tuple[int, int]
typing.Tuple[int, int]
>>> typing.Tuple[int, int] is typing.Tuple[int, int]
True
versus
>>> 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
I think the whole NameTuple implementation looks a bit half-hearted,
so I was looking to find something that is closer to the perfect
behavior of the other typing types.
Maybe I should try to implement something that keeps proper identity
like Tuple and avoids the useless function name? Like
def f() -> typing.KwTuple(x=int, y=int): ...
or
def f() -> typing.KwTuple([("x", int), ("y", int)]): ...
cheers -- Chris
On 29.07.19 18:00, Guido van Rossum wrote:
> Can't you use the proper inline form of NamedTuple?
> 
> def f() -> typing.NamedTuple("__f", [("x", int), ("y", int)]):
>   ...
> 
> seems to work.
> 
> On Mon, Jul 29, 2019 at 8:26 AM Christian Tismer <[email protected]
> <mailto:[email protected]>> wrote:
> 
> Hi friends,
> 
> I am meanwhile the PySide maintainer at The Qt Company,
> and we are trying to make the mapping from Qt functions
> to Python functions as comparable as possible.
> 
> One problem are primitive pointer variables:
> In Qt, it is natural to use "sometype *varname" to make a
> mutable variable. In Python, you have to turn such a thing
> into a result-tuple. Example:
> 
>   void QPrinter::getPageMargins(qreal *left, qreal *top, \
>     qreal *right, qreal *bottom, QPrinter::Unit unit) const
> 
> (meanwhile deprecated, but a good example)
> 
> is mapped to the Python signature
> 
>   def getPageMargins(self, \
>     unit: PySide2.QtPrintSupport.QPrinter.Unit) \
>     -> typing.Tuple[float, float, float, float]: ...
> 
> NOW my question:
> ----------------
> 
> I would like to retain the variable names in Python!
> The first idea is to use typing.NamedTuple, but I get the impression
> that this would be too much, because I would not only need to create
> an extra named tuple definition for every set of names, but also
> invent a name for that named tuple.
> 
> What I would like to have is something that looks like
> 
>   def getPageMargins(self, \
>     unit: PySide2.QtPrintSupport.QPrinter.Unit) \
>     -> typing.NamedTuple[left: float, top: float, \
>               right:float, bottom:float]: ...
> 
> but that is obviously not a named tuple.
> Possible would be to derive a name from the function, so maybe
> a definition could be used like
> 
> class PageMargingResult(NamedTuple):
>   left: float
>   top: float
>   right: float
>   bottom: float
> 
> but then I would have some opaque PageMargingResult type. This would
> work, but as said, this is a bit too much, since I only
> wanted something like a tuple with names.
> 
> What do you suggest to do here? Something what I am missing?
> 
> Cheers -- Chris
> -- 
> Christian Tismer       :^)  [email protected]
> <mailto:[email protected]>
> Software Consulting     :   http://www.stackless.com/
> Karl-Liebknecht-Str. 121   :   https://github.com/PySide
> 14482 Potsdam        :   GPG key -> 0xFB7BEE0E
> phone +49 173 24 18 776 fax +49 (30) 700143-0023
> 
> 
> 
> _______________________________________________
> Python-Dev mailing list -- [email protected]
> <mailto:[email protected]>
> To unsubscribe send an email to [email protected]
> <mailto:[email protected]>
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> 
> https://mail.python.org/archives/list/[email protected]/message/YGZELVWRGXZ5BTD4ZMATQT7IRAZVQSHR/
> 
> 
> 
> -- 
> --Guido van Rossum (python.org/~guido <http://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 -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at 
> https://mail.python.org/archives/list/[email protected]/message/LSNP3DUXGP3SD3BVMSWROAGPC67T26XE/
> 
-- 
Christian Tismer :^) [email protected]
Software Consulting : http://www.stackless.com/
Karl-Liebknecht-Str. 121 : https://github.com/PySide
14482 Potsdam : GPG key -> 0xFB7BEE0E
phone +49 173 24 18 776 fax +49 (30) 700143-0023
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/X3OBCK7UJBTN2AD3WKHAV42SRTVCUSA5/

Reply via email to