Abstract attributes

Raymond Wynne ipatrol6010 at yahoo.com
Sat Apr 24 13:12:55 EDT 2010


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




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100424/2660048c/attachment.html>


More information about the Python-list mailing list