What is a function parameter =[] for?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Nov 24 13:27:16 EST 2015


Op 24-11-15 om 18:46 schreef Terry Reedy:
> On 11/24/2015 9:34 AM, Antoon Pardon wrote:
> 
> I agree that the tutorial should talk about default argument objects (which have values) instead of conflating 'object' with 'value'.
> 
>> Op 20-11-15 om 13:12 schreef Ned Batchelder:
> 
>>> I'm not sure what your goal is at this point.  Are you:
>>>
>>>    1) still unsure what the behavior is, or
>>>    2) trying to understand why it behaves that way, or
>>>    3) hoping to change Python, or
>>>    4) trying to convince us that your language is better, or
>>>    5) something else?
>>
>> Maybe just have us recognize that some aspects of python indeed are bizarre.
> 
> The current behavior under discussion is: default argument expressions are evaluated once at function creation
> time to produce default argument objects that are bound to the function object.  I do not see this as bizarre.

I do. That a default can be mutated so that your default can have different values upon
different calls is IMO bizarre. I understand where it is coming from and I can live with
it, but I still find it bizarre.

>> That there is nothing wrong with him thinking so.
> 
> I do recognize that some would prefer that default argument expressions be treated differently, that
> 
> def f(a=expression): pass
> 
> be compiled to operate as the following does now
> 
> def f(a=None):
>     a = a if a is not None else expression
> 
> perhaps with None replaced with a hidden object() instance.

An other possibility could be compiling as the following

_tmp = expression
def f(a = None):
    a = a if a is not None else _tmp.copy()

> 
> I find the continuing fuss over the choice that was made to be the bizarre thing here.
> 

IMO that comes because the python tribe often enough can't handle differences of
appreciation. So what if BartC finds this behaviour bizarre? Why this need to
correct him of that notion? By absolutly wanting to argue that it is not bizarre
the most likely result was that BartC went on the defensive, while if people
would simply have acknowlegded that he is not the only one that finds that
behaviour bizarre and then focused on explaining what happens, a lot less fuss
would have been generated.

-- 
Antoon Pardon.



More information about the Python-list mailing list