[Python-Dev] Re: typing: how to use names in result-tuples?
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)
>>> 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 > 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]
>
> 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/YGZELVWRGXZ5BTD4ZMATQT7IRAZVQSHR/
>
>
>
> --
> --Guido van Rossum (python.org/~guido )
> /Pronouns: he/him/his //(why is my pronoun here?)/
>
>
> ___
> 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 -- p
[Python-Dev] Re: typing: how to use names in result-tuples?
On 7/29/19 4:36 PM, Christian Tismer 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? Hello, I'm afraid you're stuck with defining a new type for each of these. One reason why that's better is that info about the names will only be stored once, in the type; not in every instance. Since this is in PySide, I assume you're using the C-API. If that's the case, check out Struct Sequences, the "C equivalent of named tuples". For example, the result of "os.stat()" is a struct sequence. https://docs.python.org/3/c-api/tuple.html#struct-sequence-objects Note that like namedtuples, these are immutable, and they're proper subclasses of tuple. ___ 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/3DKGRAP7LHZGOHQON4UKXUT2TAMRSXQD/
[Python-Dev] Re: typing: how to use names in result-tuples?
On Tue, 30 Jul 2019 at 09:33, Christian Tismer wrote:
> >>> typing.NamedTuple("__f", x=int, y=int)
>
> >>> 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
___
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/OBWZHQGIJA2H2HP2VGTKHVLNY2IFJRXW/
[Python-Dev] Re: typing: how to use names in result-tuples?
I think I have to agree with Petr. Define explicit type names.
On Tue, Jul 30, 2019 at 2:45 AM Paul Moore wrote:
> On Tue, 30 Jul 2019 at 09:33, Christian Tismer
> wrote:
> > >>> typing.NamedTuple("__f", x=int, y=int)
> >
> > >>> 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?)*
___
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/GIFRTFWPEGKZ33PTW63YXKGXHHAQJ35I/
[Python-Dev] Re: typing: how to use names in result-tuples?
Hi Petr, hi Paul,
the struct sequence objects are in fact interesting!
About the "every instance" issue:
Well, if I used something like the caching approach from Paul's post,
like
cached_namedtuple = lru_cache(None)(namedtuple)
n1 = cached_namedtuple('f', ('a', 'b', 'c'))
n2 = cached_namedtuple('f', ('a', 'b', 'c'))
n1 is n2
which produces uniqueness, then I would need no explicit names.
How about that principle, but with the struct sequence objects?
Cheers -- Chris
On 30.07.19 11:13, Petr Viktorin wrote:
> Hello,
> I'm afraid you're stuck with defining a new type for each of these.
> One reason why that's better is that info about the names will only be
> stored once, in the type; not in every instance.
>
> Since this is in PySide, I assume you're using the C-API. If that's the
> case, check out Struct Sequences, the "C equivalent of named tuples".
> For example, the result of "os.stat()" is a struct sequence.
>
> https://docs.python.org/3/c-api/tuple.html#struct-sequence-objects
>
> Note that like namedtuples, these are immutable, and they're proper
> subclasses of tuple.
--
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/WL3M2UGYXO5EZ5G7OUQAYUDRLLEG7TRQ/
