What is a function parameter =[] for?

Ian Kelly ian.g.kelly at gmail.com
Fri Nov 20 11:39:10 EST 2015


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

Why would you ever want to do this in a language with properties?



More information about the Python-list mailing list