[Python-ideas] function defaults and an empty() builtin
Bruce Leban
bruce at leapyear.org
Fri May 20 21:10:55 CEST 2011
It seems to me that a better way of doing this is:
def func(optional_list=[])
optional_list = freeze(optional_list)
That is, if I expect the list to be immutable when it's empty, why wouldn't
I expect it to be immutable when it's not empty? The only case where the
immutability of the empty list matters is if there's a bug that changes the
list (or a called function changes its signature when that wasn't expected).
Wouldn't it be worth protecting against that when the list isn't empty as
well?
Of course PEP 351 was rejected so there is no freeze() builtin. (Although
personally, I don't agree with all the arguments against it. For example, I
don't think that freezing a dict has to be hashable. I also think that
immutable objects are useful in unit testing where it's allows me to easily
be sure that a passed in dict isn't changed by a function.)
Anyway, in the case of a list I suspect that this is pretty close to what
you want:
def func(optional_list=[])
optional_list = tuple(optional_list)
--- Bruce
Latest blog post: http://www.vroospeak.com Your social security number is a
very poor password
Learn how to hack web apps: http://j.mp/gruyere-security (learn how to write
buggy Python too)
On Fri, May 20, 2011 at 11:15 AM, Masklinn <masklinn at masklinn.net> wrote:
> On 2011-05-20, at 17:03 , Nick Coghlan wrote:
> > I share Steve's puzzlement as the intended use case.
> >
> > To get value from the magic empty immutable list, you will have to
> > explicitly test that calling your function with the default value does
> > the right thing.
> Why is that? The value of the empty immutable list (there's nothing magic
> to it) would be an eternal assertion that an incorrect behavior (trying
> to mutate the default parameter) can not be introduced in the function.
>
> It is no different than adding `assert` calls in the code.
>
> > But if you're writing an explicit test, having that test call the
> > function *twice* to confirm correct use of the 'is None' idiom will
> > work just as well.
> But that's the point: do you *always* use the `is None` idiom? And do
> you really love it? When you know the function body you just wrote
> does not perform any modification to the collection?
>
> There are 17 functions or methods with list default parameters and
> 133 with dict default parameters in the Python standard library.
>
> Surely some of them legitimately make use of a mutable default
> parameter as some kind of process-wide cache or accumulator, but
> I would doubt the majority does (why would SMTP.sendmail need to
> accumulate data in its mail_options parameter across runs?)
>
> Do you know for sure that no mutation of these 150+ parameters will
> ever be introduced, that all of these functions and methods are
> sufficiently tested, called often enough that the introduction of
> a mutation of the default parameter in themselves or one of their
> callees would *never* be able to pass muster?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110520/a68ce839/attachment.html>
More information about the Python-ideas
mailing list