Is it ok to type check a boolean argument?
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Wed Jan 7 15:15:20 EST 2009
Adal Chiriliuc a écrit :
> Hello,
>
> Me and my colleagues are having an discussion about the best way to
> code a function (more Pythonic).
>
> Here is the offending function:
>
> def find(field, order):
> ....if not isinstance(order, bool):
> ........raise ValueError("order must be a bool")
> ....order_by = "asc" if order else "desc"
> ....return _find(field + "+" + order_by)
>
> We are not sure what's the best practice here. Should we or should we
> not check the type of the "order" variable, which should be a bool?
This kind of typechecking is usually considered bad practice in Python,
but well, <python-zen>practicallity beats purity</python-zen> - and as a
matter of fact, if you refer to list.sort, passing a non-integer value
as the 'reverse' argument raises a TypeError...
This being said, I can only concur with other posters here about the
very poor naming. As far as I'm concerned, I'd either keep the argument
as a boolean but rename it "ascending" (and use a default True value),
or keep the 'order' name but then accept 'asc' and 'desc' as values
('asc' being the default).
My 2 cents...
More information about the Python-list
mailing list