[Python-3000] Pronouncement on parameter lists

Talin talin at acm.org
Sat Apr 22 03:34:57 CEST 2006


Guido van Rossum <guido <at> python.org> writes:

> FWIW Talin, if you're writing up a PEP for this, could I ask you to
> also specify a new introspection API? A function foo should have a
> magic attribute foo.__signature__ which provides access to the
> argument names and defaults (and types if in the future we add
> argument declarations). __signature__ objects should be copyable and
> mutable and the attribute should be assignable so that wrapping
> decorators can copy and modify the signature of the wrapped function
> onto the wrapper function (the latter is usually declared as *args,
> **kwds).

All right, sure.

BTW, does anyone have a suggestion for the name of this proposed PEP? I can 
probably get it done this evening.

As far as the __signature__ variable goes: OK, let me think about this a bit. 
First thing that strikes me is that you can't use None to indicate a lack of a 
default, since a 'None' is a valid default value.

Probably something like this: A sequence containing a tuple per argument, each 
tuple containing the name, an integer representing the number of stars (0, 1 
or 2), and optionally a default value. If there's no default then its a 2-
tuple, otherwise its a 3-tuple, so you can distinguish between the 
default=None case and the 'no default' case by looking at the length of the 
tuple.

The 'naked star' parameter is represented by (None, VARARGS) - an unnamed 
argument of the varargs type.

As far as the type of argument goes: There's no different between positional 
and keyword arguments from the recieving side, *except* that this new API 
creates such a distinction. However - all of the behaviors of the new API can 
be deduced by looking at the ordering of arguments. Thus, "keyword only" 
arguments do not need to be explicitly identified as such.

So the function signature:

   def method( a, b, *args, c=1, **kwargs ):
      ...

Would have a __signature__ variable of:

   [('a', 0),
    ('b', 0),
    ('args', 1),
    ('c', 0, 1),
    ('kwargs',2)]

As you can see, only the 'c' argument has a 3-tuple (since it has a default), 
the others are all 2-tuples. 'c' can also be deduced as a position-only 
argument, since it follows an argument a '1' in the second field.

Is that something like what you had in mind?

-- Talin




More information about the Python-3000 mailing list