[Python-ideas] Boolean parameters guidelines

M.-A. Lemburg mal at egenix.com
Mon May 9 16:00:04 EDT 2016


On 09.05.2016 21:47, Guido van Rossum wrote:
> On Mon, May 9, 2016 at 12:36 PM, Barry Warsaw <barry at python.org> wrote:
> 
>> On May 08, 2016, at 11:59 AM, Steven D'Aprano wrote:
>>
>>> I think it is preferable *not* to have boolean parameters at all.
>>>
>>> I don't remember if this is Guido's name for it, but I remember him
>>> expressing the guideline "No constant bool arguments". If you have an
>>> argument which takes a bool, and is used solely to switch between two
>>> different modes, and the caller will most likely call the function with
>>> the argument as a constant known when writing the code (rather than
>>> taking an expression or variable with value not known until runtime),
>>> then it is usually better to split the function into two, one for
>>> each mode.
>>
>> I think Guido's point is stricter than that (but I don't want to put words
>> in
>> his mouth :).  It's that a boolean flag argument shouldn't change the
>> return
>> type of the method.  Flags can certainly change the behavior of a method
>> and I
>> think that's perfect fine.
>>
> 
> These are two different things. Any time the *value* of an argument affects
> the *type* of the return value there's a problem, not just for boolean
> parameters. E.g. open(..., "rb") vs. open(..., "r") is one of the worst
> offenders -- and "r" vs. "w" is another one for open()!
> 
> The thing Steven quotes is specific to boolean parameters and pretty much
> exactly what I would say about them.

This seems overly strict, e.g. it's not uncommon to have functions
enable debugging, verbose processing or similar processing variants
using a boolean parameter.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, May 09 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list