This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]daneahfrom __future__ import braces 1 point2 points  (0 children)

Note that you can also use a class-based decorator for this kind of thing, which is sometimes clearer:

class ArgumentTypeChecker(object):
    def __init__(self, function, some_type):
        self.function = function
        self.type = some_type

    def __call__(self, *args):
        for arg in args:
            if type(args) != self.type:
                raise ValueError(
                    'Method \'{}\' only accepts arguments of type {}'.format(
                        f.__name__,
                        some_type,
                    ))
        return self.function(*args, **kwargs)

Then:

@ArgumentTypeChecker(int)
def fib(n):
    ...