[Python-3000] Minor hitch writing the Function Signature PEP

Talin talin at acm.org
Sat Apr 22 21:36:15 CEST 2006


I realized that the "Keyword-Only Arguments" and the "Function Signature"
proposals could be written as two separate PEPs, since there is only minimal
interaction between the two.

However, I started writing some use case examples for the Function Signature
PEP, and ran into a slight snag. My use case was to add preconditions to a
function, where the precondition refers to specific arguments by name:

@precondition( y=NotNegative )
def power( x, y ):
   ...

...where 'NotNegative' is a function which simply does an assert( value >= 0 ).

The general idea is that 'precondition' would take a **kwds argument, and for
each key, it would look up the correspondingly named argument in the function
signature. It would then create a wrapper function which would validate each
argument against its corresponding precondition before calling the real function.

Sounds simple enough, right?

Well the problem is that decorator functions don't have access to the machinery
that binds input arguments for formal parameters. So the wrapper function has a
hard time knowing which input arguments will be bound to which formal params,
without having to duplicate the runtime's algorithm to do this.

In order to be a generic wrapper, the wrapper will have the form:

def wrapper( *args, **kwargs ):
   ...

So where is 'y' in all of that? It could be the second argument in *args; it
could be the first argument in *args if x is passed in as a keyword argument; or
it could be in the **kwargs dict.

I'm not saying that solving this is impossible; what I am saying is that its:

   -- slow
   -- makes the example very complicated and hard to understand.

And, as we all know, if its hard to understand then its probably wrong.

-- Talin




More information about the Python-3000 mailing list