I was coding a simple abstract class for a database interface for a library I
am writing. However, it occurred to me that you can't ask for a simple
"abstract attribute", an attribute that is required for the class. Now, that
could be implemented as a property, but a getter that merely returns an
attribute adds unnecessary code. Here's a simple pure-Python implementation:
class AbcAttrMeta:
@classmethod
def __call__(cls, *args, **kwargs):
inst = cls(*args, **kwargs)
notin = list()
for n in self.__abattrs__:
if not hasattr(inst, n):
self.notin.append(n)
if notin:
raise TypeError(
"Can't instantiate abstract class {0} with abstract attributes
{1}".format(
str(type(cls))[5:-2],
', ' .join(notin))
return inst
--
http://mail.python.org/mailman/listinfo/python-list