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 -- 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/3DKGRAP7LHZGOHQON4UKXUT2TAMRSXQD/