What is a function parameter =[] for?

Marko Rauhamaa marko at pacujo.net
Fri Nov 20 11:31:50 EST 2015


Ian Kelly <ian.g.kelly at gmail.com>:

> On Fri, Nov 20, 2015 at 5:28 AM, Marko Rauhamaa <marko at pacujo.net> 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.

Consider the mutator pattern:

    def name(self, value=omitted):
        if value is omitted:
            return self._name
        self._name = value

    <URL: https://en.wikipedia.org/wiki/Mutator_method#C.2B.2B_example>


Marko



More information about the Python-list mailing list