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

Steven D'Aprano steve at pearwood.info
Fri May 20 13:54:30 CEST 2011


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? 

Most importantly, why single out an *empty* immutable list for special 
treatment, instead of providing a general immutable list type?

It seems to me that all this suggested pattern does is use a too-clever 
and round-about way of turning a buggy function into an exception for 
the caller, possibly a long way from where the error actually exists.

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



[...]
> 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.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list