[Python-Dev] Updated PEP 362 (Function Signature Object)

Yury Selivanov yselivanov.ml at gmail.com
Wed Jun 6 19:55:45 CEST 2012


Daniel,

On 2012-06-06, at 1:39 PM, Daniel Urban wrote:

>> BoundArguments Object
>> =====================
>> 
>> Result of a ``Signature.bind`` call.  Holds the mapping of arguments
>> to the function's parameters.
> 
> The Signature.bind function has changed since the previous version of
> the PEP. If I understand correctly, the 'arguments' attribute is the
> same as the return value of bind in the previous version (more or less
> the same as the return value of inspect.getcallargs). My question is:
> why we need the other 2 attributes ('args' and 'kwargs')? The
> "Annotation Checker" example uses it to call the function. But if we
> are able to call bind, we already have the arguments, so we can simply
> call the function with them, we don't really need these attributes. I
> think it would be better (easier to use and understand), if bind would
> simply return the mapping, as in the previous version of the PEP.

I'll try to answer you with the following code:

   >>> def foo(*args):
   ...    print(args)
 
   >>> bound_args = signature(foo).bind(1, 2, 3)
   >>> bound_args.arguments
   OrderedDict([('args', (1, 2, 3))])

You can't invoke 'foo' by:

   >>> foo(**bound_args.arguments)
   TypeError: foo() got an unexpected keyword argument 'args'

That's why we have two dynamic properties 'args', and 'kwargs':

   >>> bound_args.args, bound_args.kwargs
   ((1, 2, 3), {})

'BoundArguments.arguments', as told in the PEP, is a mapping to work
with 'Signature.parameters' (you should've seen it in the
"Annotation Checker" example).

'args' & 'kwargs' are for invocation.  You can even modify 'arguments'.

>> Has the following public attributes:
>> 
>> * arguments : OrderedDict
>>     An ordered mutable mapping of parameters' names to arguments' values.
>>     Does not contain arguments' default values.
> 
> Does this mean, that if the arguments passed to bind doesn't contain a
> value for an argument that has a default value, then the returned
> mapping won't contain that argument? If so, why not?
> inspect.getcallargs works fine with default values.

Yes, it won't.  It contains only arguments you've passed to the 'bind'.
The reason is because we'd like to save as much of actual context as
possible.

If you pass some set of arguments to the bind() method, it tries to map
precisely that set.  This way you can deduce from the BoundArguments what
it was bound with.  And default values will applied by python itself.

-
Yury


More information about the Python-Dev mailing list