[Python-ideas] Boolean parameters guidelines

Guido van Rossum guido at python.org
Mon May 9 16:07:41 EDT 2016


On Mon, May 9, 2016 at 1:00 PM, M.-A. Lemburg <mal at egenix.com> wrote:

> 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.
>
> 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.
>

But usually the call site values for those wouldn't be constant -- they'd
be computed from a command line flag for example. The key part of the
phrasing is "the caller will most likely call the function with the
argument as a constant known when writing the code".

FWIW I disagree with the lead-in phrase "I think it is preferable *not* to
have boolean parameters at all."

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160509/3e68e379/attachment.html>


More information about the Python-ideas mailing list