Waht do you think about my repeated_timer class

Cameron Simpson cs at cskk.id.au
Wed Feb 2 20:39:43 EST 2022


You have:

    def _check_interval(self, interval):
        if not type(interval) in [int, float]:
            raise TypeError('{} is not numeric'.format(interval))

This check is better written:

    if not isinstance(interval, (int,float)):

which handles subclasses of these types (but note that bool subclasses 
int :-) normally we don't care), or behaviourally:

    try:
        interval = float(interval)
    except ValueError as e:
        raise TypeError(
            "cannot convert %s:%r to float: %s"
            % (type(interval).__name__, interval, e)) from e

which tries to convert to float and fails if that does not work, which 
supports classes with a __float__ method (these classes are rare, but 
decimal.Decimal is one example).

I'd probably use the isinstance() form myself.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list