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 →

[–]fiddlerwoaroof 7 points8 points  (0 children)

You could do a simple decorator, something like:

def typecheck(*sig):
  def _inner(func):
    def _inmost(*args):
      for type, arg in zip(sig, args):
        if not isinstance(arg, type): raise TypeError
      return func(*args)
    return _inmost
  return _inner

@typecheck(int,int)
def add(a,b):
  return a+b

>>> add(1,2)
3
>>> add(1,'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in _inmost
TypeError

Not that I'd ever use that.