Functions as Typecheckers

Though we've done our best to anticipate real-world needs and provide a library of utility classes to accomodate them, you'll run into things in the field that we just haven't thought of. When that happens, there are two options: a) write a typecheck extention class, or b) write a function to do the job. Oftentimes, option (a) will simply be overkill. For this reason, we allow you to use functions and methods to do your typechecking work.

The way you do this is simple: in place of a type or utility class, simply pass a function, instancemethod, classmethod or staticmethod, like so:

count = 0
				
def my_typechecker(obj_to_check):
	global count
	if count < 3:
		count += 1
	else:
		return False
		
@accepts(my_typechecker)
def ...

Your function will be passed a single parameter, the object being typechecked. If your function returns False or raises an exception, that will be taken as a signal that the object has not typechecked. If your function returns anything other than False (this includes None), the object will be accepted and typechecking will continue.

Valid XHTML 1.0 Transitional