Is it ok to type check a boolean argument?

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jan 8 01:58:12 CET 2009


Scott David Daniels <Scott.Daniels at Acm.Org> writes:

> James Stroud wrote:
> > ...
> > def find(field, order_by='desc'):
> >   if order_by not in ['asc', 'desc']:
> >     raise ValueError, 'Bad order_by parameter.'
> >   ...
> I'd try a little harder with that error message.
> At least:
>     raise ValueError('Bad order_by parameter %r.' % (order_by,))
> if not:
>     raise ValueError('Bad order_by = %r (should be in %r).' % ( 
> 
>                               order_by, ['asc', 'desc']))

Why are people so reluctant to make error message templates clearer
with named placeholders?

    valid_order_by_values = ['asc', 'desc']
    if order_by not in valid_order_by_values:
        raise ValueError(
            'Bad order_by parameter %(order_by)r, should be in %(valid_order_by_values)r'
            % vars())

-- 
 \       “I love and treasure individuals as I meet them, I loathe and |
  `\     despise the groups they identify with and belong to.” —George |
_o__)                                                     Carlin, 2007 |
Ben Finney



More information about the Python-list mailing list