[PyQt] thoughts about dip interfaces and python abstract base classes

Darren Dale dsdale24 at gmail.com
Sun Mar 13 19:01:53 GMT 2011


I've been learning about python's abstract base classes at
http://www.python.org/dev/peps/pep-3119/ and
http://docs.python.org/py3k/library/abc.html . The PEP mentions that
there is considerable overlap between ABCs and interfaces, and I'm
rereading the dip documentation on interfaces now. If I understand
correctly, dip's "implements" decorator is similar (perhaps based on)
python's abstract base class "register" class method. Is it ever the
case that dip classes actually subclass a dip interface, as python
classes would subclass one or more abstract base class?

Also, this morning I suggested on python-ideas a small tweak to
python's builtin "property" which would allow an abstract properties
to be declared using the decorator syntax. I thought it might be of
interest to dip:

import abc

class Property(property):

    def __init__(self, *args, **kwargs):
        super(Property, self).__init__(*args, **kwargs)
        if (getattr(self.fget, '__isabstractmethod__', False) or
            getattr(self.fset, '__isabstractmethod__', False) or
            getattr(self.fdel, '__isabstractmethod__', False)):
            self.__isabstractmethod__ = True

class C(metaclass=abc.ABCMeta):
    @Property
    @abc.abstractmethod
    def x(self):
        return 1
    @x.setter
    @abc.abstractmethod
    def x(self, val):
        pass

try:
    # Can't instantiate C, C.x getter and setter are abstract
    c=C()
except TypeError as e:
    print(e)

class D(C):
    @C.x.getter
    def x(self):
        return 2

try:
    # Can't instantiate D, the D.x setter is still abstract
    d=D()
except TypeError as e:
    print(e)

class E(D):
    @D.x.setter
    def x(self, val):
        pass

# E is now instantiable
print(E())


More information about the PyQt mailing list