[Python-ideas] deferred default arguments

Eric Snow ericsnowcurrently at gmail.com
Thu Jul 14 00:43:09 CEST 2011


On Wed, Jul 13, 2011 at 3:36 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 7/13/2011 3:26 PM, Eric Snow wrote:
>
>> class X:
>>     def f(self, name="N/A"):
>
>         print(name)
>>
>> class Y(X):
>>     def f(self, name="N/A"):
>>         super().f(name)
>
> I believe
>
> class Y(X):
>    def f(self, name=None):
>        super().f(name)
>    f.__defaults__ = X.f.__defaults__
>
> will more or less do what you want. Using 'super()' instead of 'X' does not
> seem to work. The default replacement might be done with a function or class
> decorator.

Yeah, but if the defaults of X.f get changed at runtime, after the
definition of Y, the defaults for Y.f will likely be out of sync.
Also, usually I want to be selective about which defaults I assume.
Following your recommendation would involve calculating a new
__defaults__ and a new __kwdefaults__.  PEP 362 would make this
easier, but probably won't be done for a while.

However, you're right that a decorator could probably handle this.
The downside is that it would not be a trivial decorator to get what I
am after.  Thanks for the feedback.

-eric

>
>> Y().f()
>
> prints 'N/A'
>
> --
> Terry Jan Reedy
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list