Uh oh!
There was an error while loading. Please reload this page.
PEP 590: allow passing a dict to PyObject_Vectorcall - #1038
Conversation
encukou
commented
May 9, 2019
The argument against is that we want to position |
jdemeyer
commented
May 9, 2019
That's just a matter of documentation: we could say that a tuple is recommended.
There is an obvious advantage over An important note is also that it's not just a matter of performance but also of convenience: by allowing various kinds of calls, we make it as easy as possible for the users of the API. There are various existing use cases for using a vector of args but a dict of kwargs (that's the reason why |
encukou
commented
May 9, 2019
Also, this would not be hard to add afterwards, if we find compelling use cases. |
scoder
commented
May 10, 2019
I agree that optionally allowing a dict would be nice. Some callers might simply already have a dict lying around (if only for legacy reasons to support older Python versions), in which case forcing them to either create a pos-args tuple and use |
jdemeyer
commented
May 10, 2019
I don't get this argument. In any case, it seems more like a documentation issue: we could document that a tuple is recommended.
We don't need to find use cases, there are already use cases in the CPython source code. To name two random use cases, it's used for implementing |
encukou
commented
May 10, 2019
That convinces me that |
markshannon
commented
May 11, 2019
I think this is a bad idea, as it suggests to the user that passing a dictionary to The first reason is that The second reason is that it is not clear what
To answer @scoder's comment about caller having a dictionary "lying around". If they do so for legacy call reasons, they should also have tuple "lying around" and can just use the Would an API function to help convert from the vector/dict form proposed by this PR to the |
jdemeyer
commented
May 11, 2019
As I already said, that's just a documentation issue... |
jdemeyer
commented
May 11, 2019
Also read what I posted earlier: it's not only a matter of performance, but also of convenience. With this in mind, it shouldn't be a problem to accept a |
jdemeyer
commented
May 13, 2019
But that's an argument in favor of this PR. When you don't allow passing a dict in |
encukou
commented
May 17, 2019
If the caller knows the callee, it should use the right protocol. So, let's assume it doesn't know the callee (and doesn't want to introspect it). Now,
|
jdemeyer
commented
May 17, 2019
I should add that I've been using this |
markshannon
commented
May 18, 2019
I'm unconvinced that this "have a vector + dict lying around" is a real scenario, and it is definitely not a common one. The purpose of PEP 590 is to add one new calling convention. Allowing PEP 590 is not about convenience or ease of use. It is about performance. Vagueness in the API is likely to introduce confusion and inefficiency. I think that accepting two fundamentally different kinds of things for a single function is poor API design. The change in the second argument implicitly changes the type of first argument. If the second argument is a tuple, then the first argument is a vector of all the argument values. However if the second argument were to be a dict, then the first argument becomes a vector of just the positional arguments. Which seems needlessly confusing and error prone. If the ability to pass vector+dict is that valuable, which I doubt, then use |
I'm fine creating a second function for the vector + dict calling convention, if that's the compromise. But I would put those two functions on the same level (i.e. make both public, calling them |
Could you please explain
|
jdemeyer
commented
May 18, 2019
Counter-proposal at #1057 with the same functionality as this PR but split over two different API functions. |
Allow passing a dict as
keywordsargument toPyObject_Vectorcall. Since checking for atupleordictis very fast, this costs almost nothing in terms of performance.The fact that
_PyObject_FastCallDict(which does vectorcall with a dict) exists and is used in various places in CPython shows that such functionality is useful.CC @encukou@markshannon