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

Masklinn masklinn at masklinn.net
Fri May 20 08:37:30 CEST 2011


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.

The idea is rather similar to Java's Collections.empty* (emptySet,
emptyMap and emptyList), which could be fine solutions to this issue
indeed. There is already `frozenset` for sets, but there is no way to
instantiate an immutable list or dict in Python right now, as far as I
know (tuples don't work as several mixed list&tuple operations yield
an error, and I don't like using tuples as sequences personally).

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.



More information about the Python-ideas mailing list