Fredrik Tolf wrote: > If I have a variable which points to a function, can I check if certain > argument list matches what the function wants before or when calling it? > > Currently, I'm trying to catch a TypeError when calling the function > (since that is what is raised when trying to call it with an illegal > list), but that has the rather undesirable side effect of also catching > any TypeErrors raised inside the function. Is there a way to avoid that? > > Fredrik Tolf > >
Since python is "weakly typed", you will not be able to check what "type" of arguments a function expects. TypeErrors relating to the type of argemtns you pass will be raised inside the function only and not when it is called. I.e. there is no type checking when a function is called, only in its body. Types erros can be raised when calling a function like this: func(*avar) or func(**avar) In the former case, if avar is not a sequence, you will get a TypeError. In the latter case, if avar is not a dictionary, you will get a TypeError. In this latter case, if your dictionary has keys that are not in the parameter list, you will get a TypeError. This would be similar to explicitly specifying a keyword argument that is not in the parameter list (e.g. func(aparam=someval)). TypeError will be raised also if you give an improper number of arguments to the function. So, if you want to know the number of arguments expected, I've found this works: py> def func(a,b,c): ... print a,b,c ... py> func.func_code.co_argcount 3 To check the parameter names, use py> func.func_code.co_names ('a', 'b', 'c') James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list