Checking function's parameters (type, value) or not ?
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Apr 6 09:29:02 EDT 2016
On 06/04/2016 14:07, ast wrote:
> Hello
>
> I would like to know if it is advised or not to test
> a function's parameters before running it, e.g
> for functions stored on a public library ?
>
> Example:
>
> def to_base(nber, base=16, use_af=True, sep=''):
>
> assert isinstance(nber, int) and nber >= 0
> assert isinstance(base, int) and base >= 2
> assert isinstance(use_af, bool)
> assert isinstance(sep, str) and len(sep) == 1
>
> tbc
>
> With these tests, you are sure that the function to_base is
> well used. But it slows down the program.
> Without, python interpreter may crash later in the function
> or worse provide a meaningless result.
>
> What library designers do ?
>
Please see
http://ftp.dev411.com/t/python/python-list/13bhcknhan/when-to-use-assert
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list