What is a function parameter =[] for?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Nov 24 09:18:43 EST 2015


Op 20-11-15 om 02:05 schreef Steven D'Aprano:
> On Fri, 20 Nov 2015 04:30 am, BartC wrote:
> 
>> On 19/11/2015 16:01, Steven D'Aprano wrote:
> [...]
> 
>> The whole concept of 'mutable' default is alien to me. A default is just 
>> a convenient device to avoid having to write:
>>
>>    fn(0) or fn("") or fn([])
> 
> Says who?
> 
> Here's another use for function defaults, as static storage:
> 
> 
> # Ackermann's function
> def ack(m, n, _memo={}):
>     key = m, n
>     if key not in _memo:
>         if m==0: v = n + 1
>         elif n==0: v = ack(m-1, 1)
>         else: v = ack(m-1, ack(m, n-1))
>         _memo[key] = v
>     return _memo[key]
> 
> 
> This is a quick and easy way to memoise a function which would otherwise be
> horribly slow. And it only works because _memo is bound to a mutable object
> once, and once only.

I think this is the worst kind of example you can give in defense for this
kind of behaviour. Because you are defining a function as having three parameters
where it really has only two. You really don't want this function to be called
with that third parameter.

Secondly, because we have decorators now, it really is not a good idea to define
your function with the memoization in the body functionality.

Thirdly your use with a default argument would go horribly wrong, should we
try to use it in the decorator definition.

-- 
Antoon.




More information about the Python-list mailing list