[Python-ideas] Boolean parameters guidelines

M.-A. Lemburg mal at egenix.com
Wed May 11 08:31:28 EDT 2016


On 11.05.2016 02:20, Guido van Rossum wrote:
> In any case the discussion was more about how to design the API in the
> first place than about what the call should look like. In particular, if
> the raise_errors parameter is nearly always given as the literal True or
> False, it might be better to have
> 
>   process(data)
> 
> and
> 
>   process_raising_errors(data)

This would work in the simple case where the try-except is
used on the whole inner processing block (as in the example
I posted), but in practice the variant you want to make
selectable often requires adjusting multiple code sections or
paths in the block.

By having two separate functions, you'd then create a lot of
duplicated code (with all the possibly issues that go with
it, e.g. copy&paste errors, bugs only fixed in one copy,
poor maintainability, etc.).

Example:

def complex_process(data, raise_errors=True):

    try:
        ...phase 1...
    except ValueError:
        if raise_errors:
            raise
        else:
            return None

    ...phase 2...

    try:
        ...phase3...
    except TypeError:
        if raise_errors:
            raise
        else:
            return None

    return result

The obvious solution would be to wrap the raising variant
in an outer function which catches the exceptions, but in
the above example, you'd then have to catch both ValueError
and TypeError, which may actually catch more errors than
you really want to catch.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, May 11 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