Access the methods of a class
aleax at aleax.it
Sat Nov 1 11:24:23 CET 2003
Terry Reedy wrote:
>> I have a base class with a 'sanity-check' method. This method should
>> through all the methods and check if they are all 'thunks' (zero
>> functions, well actually, 1 parameter: self).
> If you want to enforce 'sanity' rather than post-check, you might be
> able to use a custom metaclass -- which gets the dictionary of
> attributes as an argument. But that is an expert project not for the
> faint of heart.
I am not a cardiologist, but I think you're emphasizing the difficulties
too much. Suppose we have a checking function that does the "atomic"
check on one function-that's-about-to-become-a-method (I showed how to
do that with inspect in a previous post) -- say a function 'dockeck'
which is called with the name and corresponding functionobject and raises
an appropriate exception if they're somehow "not right". Then packaging
the use of this function in a custom metaclass is not hard at all:
def __new__(mcl, clasname, clasbases, clasdict):
for name, value in clasdict.iteritems():
if callable(value): docheck(name, value)
return type.__new__(mcl, clasname, clasbases, clasdict)
class Checked: __metaclass__ = MetaChecker
that's all -- just inherit your classes from Checked rather than from object
and all of the classes' callable attributes will be subject to whatever
checking docheck performs at class-object-creation time. (Easy to tweak if
you don't want to check all callables, again see standard module inspect
for what you can easily find out about the items in clasdict).
For anybody doing reasonably advanced things such as reflection and the
like -- and the original poster did indicate that such things were exactly
his goal -- it does not seem to me that such well-bounded and simple use
of a custom metaclass should prove forbiddingly hard or heart-threatening.
More information about the Python-list