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

Masklinn masklinn at masklinn.net
Fri May 20 21:18:30 CEST 2011


On 2011-05-20, at 21:22 , Ethan Furman wrote:
> Masklinn wrote:
>> On 2011-05-20, at 20:56 , Ethan Furman wrote:
>>> Masklinn wrote:
> >>> Ethan wrote:
>>>>> If the function can't proceed properly without an actual parameter, why supply
>>>>> a default?
> >>>
>>>> It can, where did you get the idea that it could not? That's the point of the
>>>> default parameter.
> >>
>>> Yes, I am aware.  And the point of providing an empty list as a default is so you have a list to add things to
> >
>> Not at all, you may just want to iterate on it, or accumulate it. There are cases of exactly this in the standard library itself.
> 
> Um, isn't accumulating modifying?
No. `reduce` does not alter the list in place, nor does `sum`, `any` or iterating on the list.

>  Or do you mean accumulating in a global or class instance?
Accumulating can be done in anything.

> And why would you iterate over an empty list?
Because you're iterating period, and that it's an empty list has no influence on your behavior. You'll simply do nothing during your iteration, because the iteration count will be 0. Why special-case empty lists when there is no need to?

Same with dict, `get` works on empty dicts as well as on any other such collection.

>  If you have an example from the stdlib I'd love to see it (seriously -- I'm always up for learning something).

Mailcap does that line 170: `subst` takes an empty list as a default parameter, forwards that parameter to `findparam` which iterates on the list to try and find the param.

If it can't find the param in the list, it simply returns an empty string.

An empty list is simply a case where it will never find the param, and it will Just Work. No need to create a special case.

Have you really never done such a thing?


More information about the Python-ideas mailing list