[Python-Dev] pep 362 - 5th edition

Nick Coghlan ncoghlan at gmail.com
Wed Jun 20 06:55:54 CEST 2012


On Wed, Jun 20, 2012 at 1:51 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> What if instead of 'optional', we have 'base_signature'
> (or 'from_signature')?
>
>    sig = signature(func)
>    params = OrderedDict(tuple(sig.parameters.items())[1:])
>    new_sig = Signature(params, base_signature=sig)
>
> And for Paramater:
>
>    param = sig.parameters['foo']
>
>    param1 = Parameter('bar', base_parameter=param)
>    param2 = Parameter('spam', annotation=int, base_parameter=param)
>    param3 = Parameter(base_parameter=param)
>    param4 = Parameter(default=42, base_parameter=param)
>
> So 'base_parameter' will be a template from which Parameter's constructor
> will copy the missing arguments.

Good thought (and better than my initial idea), but I'd follow the
model of namedtuple._replace and make it a separate instance method:

    sig = signature(f)
    new_sig = sig.replace(parameters=sig.parameters.values()[1:])

    param = sig.parameters['foo']
    param1 = param.replace(name='bar')
    param2 = param.replace(name='spam', annotation=int)
    param3 = param.replace() # namedtuple._replace also allows this edge case
    param4 = param.replace(default=42)

Such a copy-and-override method should be the last piece needed to
make immutable Signature objects a viable approach.

Cheers,
Nick.


-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list