Problem with extremely small real number

Roberto Bonvallet rbonvall at gmail.com
Mon Sep 3 19:04:57 CEST 2007


On 3 sep, 10:56, Andrea <aciru... at gmail.com> wrote:

> def binomial(n, k):
>         assert n>0 and isinstance(n, (int, long)) and isinstance(k,
> (int,long))

Don't use assert to check whether the parameters have the right value.
assert should be used to claim that a certain condition always hold in
your program.
Prefer the following form, which will make debugging clearer:

    if n <= 0:
        raise ValueError("Wrong value of n")
    if not isinstance(n, (int, long)):
        raise TypeError("Wrong type of n")
    if not isinstance(k, (int, long)):
        raise TypeError("Wrong type of k")

Anyway, you shouldn't be checking the type of the parameters either.
It could be more useful if you cast to the desired type.  This way,
your function will handle gracefully the case when you accidentally
pass a string as a parameter:

    n = int(n)
    k = int(k)
    if n <= 0:
        raise ValueError("Wrong value of n: %d" % n)

Best regards,
--
Roberto Bonvallet




More information about the Python-list mailing list