def require_abstract_fields(obj, cls): abstract_fields = getattr(cls, "abstract_fields", None) if abstract_fields is None: return for field in abstract_fields: if not hasattr(obj, field): raise RuntimeError, "object %s failed to define %s" % (obj, field) class a(object): abstract_fields = ("x", ) def __init__(self): require_abstract_fields(self, a) class b(a): abstract_fields = ("y", ) x = 5 def __init__(self): require_abstract_fields(self, b) super(b, self).__init__() b() a()
Pay attention to the class type passing in require_abstract_fields , so if it is used by several inherited classes, they do not all check the fields of the derived class itself. You may be able to automate this with a metaclass, but I did not understand this. Field definition in None accepted.
Glenn Maynard Jul 20 '09 at 0:08 2009-07-20 00:08
source share