[Python-ideas] DBC (Re: Explicit variable capture list)

Petr Viktorin encukou at gmail.com
Tue Jan 26 11:17:55 EST 2016


On 01/26/2016 04:51 PM, Chris Angelico wrote:
> On Wed, Jan 27, 2016 at 2:42 AM, Paul Moore <p.f.moore at gmail.com> wrote:
>> On 26 January 2016 at 15:24, Chris Angelico <rosuav at gmail.com> wrote:
>>> This still
>>> has one nasty problem though: the requires and ensures functions can't
>>> see function arguments.
>>
>> See my code - you can put the args onto the instance as attributes for
>> requires/ensures to inspect.
> 
> Except that there can be only one of those at any given time, so you
> run into issues with recursion or threads/async/etc; plus, it's still
> not properly clean - you have to check either args or kwargs,
> depending on whether the argument was passed positionally or by
> keyword. I don't see that as a solution.
> 
> (Maybe what we need is a "keyword-to-positional" functools feature -
> anything in **kwargs that can be interpreted positionally gets removed
> and added to *args. Or the other way - keywordify everything.)

Well, it's not in functools.

import inspect

def keyword_to_positional(func, args, kwargs):
    sig = inspect.signature(func).bind(*args, **kwargs)
    sig.apply_defaults()
    return sig.args, sig.kwargs

def keywordify_everything(func, args, kwargs):
    sig = inspect.signature(func).bind(*args, **kwargs)
    sig.apply_defaults()
    return sig.arguments



More information about the Python-ideas mailing list