What is a function parameter =[] for?

Chris Angelico rosuav at gmail.com
Fri Nov 20 11:24:07 EST 2015


On Sat, Nov 21, 2015 at 3:16 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> One could argue that you should always use a sentinel object for default
>> values. That also allows you to distinguish between omitted values and
>> default values:
>>
>>    def asklist(caption, data, n=omitted, rows=omitted, width=omitted,
>>                flags=omitted, buttons=omitted, tablist=omitted,
>>                heading=omitted):
>>
>> but that would be rather pedantic in most circumstances.
>
> I think that would be bad design; in general, you shouldn't *need* to
> distinguish whether the value was omitted, because it should always be
> possible to explicitly pass the default value.

The cases where that's not true are usually ones that are more like
C++ overloaded functions:

def next(iter):
    return iter.__next__()
def next(iter, default):
    try: return iter.__next__()
    except StopIteration: return default

You cannot have any actual object to represent the default state, for
the same reasons that you can't have __next__ return a magic value to
represent "no more values". That's why, in the proposed semantics for
an explicit late-binding syntax, I compare with *args notation, which
_can_ let you distinguish perfectly.

ChrisA



More information about the Python-list mailing list