[Python-Dev] signature object issues (to discuss while I am out of contact)

Edward Loper edloper at gradient.cis.upenn.edu
Mon May 1 20:40:31 CEST 2006


Brett Cannon wrote:
> The second question is whether it is worth providing a function that
> will either figure out if a tuple and dict representing arguments
> would work in calling the function.  Some have even suggested a
> function that returns the actual bindings if the call were to occur. 
> Personally I don't see a huge use for either, but even less for the
> latter version.  If people have a legit use case for either please
> speak up, otherwise I am tempted to keep the object simple.

One use case that comes to mind is a type-checking decorator (or 
precondition-checking decorator, etc):

@precondition(lambda x,y: x>y)
@precondition(lambda y,z: y>z)
def foo(x, y, z): ...

where precondition is something like:

def precondition(test):
     def add_precondition(func):
         def f(*args, **kwargs):
             bindings = func.__signature__.bindings(args, kwargs)
             if not test(**bindings):
                 raise ValueError, 'Precontition not met'
             func(*args, **kwargs)
         return f
     return add_precondition

-Edward


More information about the Python-Dev mailing list