[Python-ideas] Default arguments in Python - the return - running out of ideas but...
Chris Rebert
pyideas at rebertia.com
Fri May 15 08:13:23 CEST 2009
On Thu, May 14, 2009 at 9:16 PM, Tennessee Leeuwenburg
<tleeuwenburg at gmail.com> wrote:
> A thought from another direction...
>
> Any chance we could have the interpreter raise a warning for the case
>
> def foo(a = []):
> #stuff
>
> ?
>
> The empty list and empty dict args would, I imagine, be the two most common
> mistakes. Showing a warning might, at least, solve the problem of people
> tripping over the syntax.
+1 on throwing a ValueError for non-hash()-able (and thus probably
mutable) default argument values. It's by no means perfect since
objects are hash()-able by default using their ID, but it would at
least help in the frequent "well-behaved mutable container object"
cases.
The barrier to this idea would be the code breakage involved; IMHO,
code exploiting mutable defaults as static variables is in poor style
anyway, but backward compatibility is a significant concern of the
BDFL and Python devs; though I would hope the breakage might be seen
as justifiable in this case.
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-ideas
mailing list