This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Parameter doesn't expose its index
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, larry, pitrou, yselivanov
Priority: normal Keywords:

Created on 2015-05-14 09:51 by pitrou, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg243166 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-05-14 09:51
A signature Parameter object only exposes its name, not its index in the signature. I think that would be a useful information to have.
msg243195 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-14 15:40
Do you have any good use case for this?

In one of the first iterations of PEP 362 we had Parameter.index. However, we later redesigned the object to work as a building block -- immutable, and explicitly detached from its parent Signature.  This way there is nothing wrong in taking a parameters from one signature, and using them to build a new one.  And since (I think) I'm doing this kind of things in my own code, adding this attribute (or even reference to the parent Signature) to Parameter might break things, or even introduce strange side effects.

Another reason is that we don't preserve the order of keyword arguments.  And having things like '*args' further disconnects parameters indexes from arguments indexes.

We can add an 'index(name)' or 'index(param)' method to the Signature class, but I don't know any good use case why would we need that.
msg243199 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-05-14 15:44
Le 14/05/2015 17:40, Yury Selivanov a écrit :
> 
> Do you have any good use case for this?

Passing a parameter around without having to pass the index separately :-)

> In one of the first iterations of PEP 362 we had Parameter.index.
However, we later redesigned the object to work as a building block --
immutable, and explicitly detached from its parent Signature. This way
there is nothing wrong in taking a parameters from one signature, and
using them to build a new one.

I see. That might be annoying in that case, indeed.

> Another reason is that we don't preserve the order of keyword
arguments.

What do you mean? In Signature or in BoundArguments? I would hope that
Signature keeps it.

> And having things like '*args' further disconnects parameters
> indexes from arguments indexes.

Not necessarily. In my case, I treat a stararg parameter as a single
parameter.
msg243201 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-14 15:49
> What do you mean? In Signature or in BoundArguments? I would hope that
Signature keeps it.

I mean during the actual call, as **kwargs aren't ordered.

I think having indexes for parameters would make sense for a language like JS or C, where there are no keyword arguments, and indexes of parameters match indexes of arguments.
msg243203 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-05-14 15:58
Le 14/05/2015 17:49, Yury Selivanov a écrit :
> 
>> What do you mean? In Signature or in BoundArguments? I would hope
>> that
> Signature keeps it.
> 
> I mean during the actual call, as **kwargs aren't ordered.
> 
> I think having indexes for parameters would make sense for a language
> like JS or C, where there are no keyword arguments, and indexes of
> parameters match indexes of arguments.

As mentioned in the issue, when re-implementing function calls, you have
to flatten the arguments into a simple argument list (because the
function parameters are actually a sequence (*), despite Python's rich
function call possibilities).

(*) at least for pure Python functions, where the arguments are simply
pushed sequentially on the ceval stack
msg243206 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-14 16:02
> As mentioned in the issue, when re-implementing function calls, you have
to flatten the arguments into a simple argument list [..]

Then you probably need indexes for BoundArguments, not Parameters.


> (*) at least for pure Python functions, where the arguments are simply
pushed sequentially on the ceval stack

TBH I think this is a very special use case.  I'm -0.5 on including this to the stdlib.
msg243209 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-05-14 16:29
Given the drawback you mentioned above, I agree that this may be a hard sell :)
msg243213 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-14 18:16
OK, I'm closing this one.
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68377
2015-05-14 18:16:10yselivanovsetstatus: open -> closed
resolution: rejected
messages: + msg243213

stage: resolved
2015-05-14 16:29:14pitrousetmessages: + msg243209
2015-05-14 16:02:27yselivanovsetmessages: + msg243206
2015-05-14 15:58:32pitrousetmessages: + msg243203
2015-05-14 15:49:44yselivanovsetmessages: + msg243201
2015-05-14 15:44:11pitrousetmessages: + msg243199
2015-05-14 15:40:44yselivanovsetnosy: + brett.cannon, larry
messages: + msg243195
2015-05-14 09:51:42pitroucreate