[Python-ideas] proto-PEP: Fixing Non-constant Default Arguments

Roman Susi rnd at onego.ru
Tue Jan 30 20:48:57 CET 2007


Chris Rebert wrote:
> Roman Susi wrote:
> 
>> Hello!
>>
>> I'd liked to say outright that this bad idea which complicates matters

[skip]

> 
>> P.S. However, I may be wrong. In that case my syntax suggestion would
>> be this:
>>
>> def foo(non_const or []):
>>    ...
>>
>> where [] is executed at runtime BECAUSE at def time non_const is
>> somehow True and that is enough to leave [] alone.
>> I have not checked, but I believe it is backward compatible.
>> Anyway, could you summarize both contr-argument and this syntax
>> proposal in the PEP?
> 
> 
> I don't quite understand exactly how this would work and would like more
> details on it, but once you've explained it, of course I'd be happy to
> include it in the next draft.

Simple.


def foo(non_const or []):
    ...

is equivalent to

def foo(non_const=None):
    if non_const is None:
        none_const = []
    ...


And this will be as before:

def foo(non_const=[]):
    ...

Also, I thing that programmers should not use subtle difference between
None and other False values, so something like

def foo(non_const=None):
    non_const = none_const or []

is also valid.

Another approach (if you want to pursue the feature) could be
complication to name binding protocol.

a = []

will be as before, but default value assignment could trigger some extra
method. So, you can explicitly regulate your instance reaction to
default-value assignment:

class MyMutableList:
    ...
    def __default__(self, old_default):
        return old_default.copy()




Regards,
Roman


> - Chris Rebert
> 




More information about the Python-ideas mailing list