functions, list, default parameters
Ian
ian.g.kelly at gmail.com
Tue Nov 2 13:34:27 EDT 2010
On Nov 2, 5:59 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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 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
example:
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.
Cheers,
Ian
More information about the Python-list
mailing list