Trouble with metaclass

Fernando Rodriguez frr at
Thu Nov 13 12:27:03 CET 2003


I'm having trouble with a metaclass suposed to check the method signature of
its classes.

Here's the metaclass:

class MetaChecker(type):
    def __new__(cls, name, bases, attribs):
        for name, value in attribs.iteritems():
            if  inspect.ismethod(value):
                if not isSingleArg(value):
                    raise "%s is not a thunk method!"%name
        return type.__new__(cls, name, bases, attribs)
def isSingleArg(fn):
    args = inspect.getargspec(fn)[0]
    if len(args) == 1:
        return 1
        return None

And here's a class with this metaclass:

class Preconditions (object):
    __metaclass__ = MetaChecker

If I define a descendant of Preprocesor with a method with 2 arguments, I
don't get any error:
class P (Preprocessor):
   def f(self, x):
      return x

What am I doing wrong? O:-)

More information about the Python-list mailing list