functions, list, default parameters

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Nov 2 07:59:25 EDT 2010


On Tue, 02 Nov 2010 22:06:40 +1300, Lawrence D'Oliveiro wrote:

> In message <mailman.481.1288683620.2218.python-list at python.org>, Chris
> Rebert wrote:
> 
>> On Fri, Oct 22, 2010 at 12:36 AM, Steven D'Aprano
>> <steve at remove-this-cybersource.com.au> wrote:
>>>
>>> Default mutable arguments have their place
>> 
>> But it's a rather obscure one where it is almost never strictly
>> necessary to venture.
> 
> Mediocre programmers with a hankering towards cleverness latch onto it
> as an ingenious way of maintaing persistent context in-between calls to
> a function, completely overlooking the fact that Python offers much more
> straightforward, comprehensible, flexible, and above all maintainable
> ways of doing that sort of thing.

Perhaps.

Or maybe it's the mediocre programmers who find mutable defaults 
confusing, incomprehensible and unmaintainable.

Certainly it's the mediocre programmers who seem to be incapable of 
understanding that Python has no way of telling whether arbitrary objects 
are mutable or not. 

def foo(x, y=list()):
    pass

Is y a mutable default or not?

For the benefit of any mediocre programmers out there, be careful before 
you answer. This *is* a trick question.


-- 
Steven



More information about the Python-list mailing list