[Python-ideas] function defaults and an empty() builtin

Masklinn masklinn at masklinn.net
Fri May 20 14:14:09 CEST 2011


On 2011-05-20, at 13:54 , Steven D'Aprano wrote:
> On Fri, 20 May 2011 04:37:30 pm you wrote:
>> On 2011-05-20, at 07:57 , Steven D'Aprano wrote:
>>> But as I said, I don't think this will fly. What's the point? If
>>> you don't pass an argument for optional, and get a magic empty
>>> list, your function will raise an exception as soon as it tries to
>>> do something with the list. To my mind, that makes it rather
>>> useless. If you want the function to raise an exception if the
>>> default value is used, surely it's better to just make the argument
>>> non-optional.
>>> 
>>> But perhaps I've misunderstood something.
>> 
>> That Jack's object would be an empty, immutable collection. Not an
>> arbitrary object.
> 
> Yes, I get that, but what's the point? What's an actual use-case for it?
> What's the point of having an immutable collection that has the same 
> methods as a list, but raises an exception if you use them? 
Not if you use them, if you *modify* them.

I'm guessing the point is to be able to avoid the `if collection is None`
dance when the collection is not *supposed* to be modified: an immutable
collection would immediately raise on modification, acting as a
precondition/invariant and ensuring mutation is not introduced on the
original collection.

> Most importantly, why single out an *empty* immutable list for special 
> treatment, instead of providing a general immutable list type?
I'm pretty sure I mentioned that as a good idea in the following two
paragraphs of my comment.

But in Jack's case, I'm guessing it's because the Python bug of
collections-as-default-values is most generally encountered with empty
collections.

> I can't think of any reason I would use this special empty() value as a 
> default instead of either:
> 
> - fix the function to not use the same default list; or
> - if using a default value causes problems, don't use a default value
But that's the very issue: the mutable-collection-default is a common
bug, and one which may be quite hard to debug in the long term (not just
that, but it may not even be visible as a bug — even though data is
corrupted — and manifest itself as an even harder to track memory leak).

By making that default-empty-collection immutable, mutations of the
default-argument collection become obvious (they blow up), and the
function can be fixed. It's much easier to track this down than a
strange memory leak.

On 2011-05-20, at 13:54 , Steven D'Aprano wrote:
>> The ability to either make a collection (list or dict, maybe via
>> separate functions) immutable or to create a special immutable empty
>> variant thereof would work nicely.
> These are two different issues. Being able to freeze an object would be 
> handy, but a special dedicated empty immutable list strikes me as 
> completely pointless.

An immutable empty collection can be a system-wide singleton, and
extremely cheap to use. It makes for a good default value or default
object member when you expect the collection to never be modified.

Using `collections.empty_list` is also more readable and clearer than,
say, `collections.freeze([])`.


More information about the Python-ideas mailing list