functions, list, default parameters
ian.g.kelly at gmail.com
Tue Nov 2 18:34:27 CET 2010
On Nov 2, 5:59 am, Steven D'Aprano <st... at REMOVE-THIS-
> 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()):
> Is y a mutabledefaultor not?
> For the benefit of any mediocre programmers out there, be careful before
> you answer. This *is* a trick question.
I fail to see your point. You might as well argue that Python has no
way of knowing whether it should raise a TypeError in the following
my_tuple = (1, 2, 3) + list(xrange(4, 7))
Dynamic typing means that these sorts of checks must be delayed until
runtime, but that doesn't make them useless or impossible.
It seems to me that there is a rather simple case to be made for
allowing mutable default arguments: instances of user-defined classes
are fundamentally mutable. Disallowing mutable default arguments
would mean disallowing instances of user-defined classes entirely.
That would be excessive and would violate Python's general rule of
staying out of the programmer's way.
More information about the Python-list