Extending both an abstract base class and a class derived from "object" works as you would expect: if you you haven't implemented all abstract methods and properties, you get an error.
Strangely, replacing the object-derived class with an class that extends "Exception" allows you to create instances of classes which do not implement all the required abstract methods and properties.
For example:
import abc
# The superclasses
class myABC( object ):
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def foo(self):
pass
class myCustomException( Exception ):
pass
class myObjectDerivedClass( object ):
pass
# Mix them in different ways
class myConcreteClass_1(myCustomException, myABC):
pass
class myConcreteClass_2(myObjectDerivedClass, myABC):
pass
# Get surprising results
if __name__=='__main__':
a = myConcreteClass_1()
print "First instantiation done. We shouldn't get this far, but we do."
b = myConcreteClass_2()
print "Second instantiation done. We never reach here, which is good."
...yields...
First instantiation done. We shouldn't get this far, but we do.
Traceback (most recent call last):
File "C:/Users/grahamf/PycharmProjects/mss/Modules/mssdevice/sutter/sutter/test.py", line 28, in <module>
b = myConcreteClass_2()
TypeError: Can't instantiate abstract class myConcreteClass_2 with abstract methods foo
I know that "Exception" and therefore "myCustomException" have no attribute "foo", so why am I getting away with instantiating "myCustomException"?
EDIT: For the record, this is the hackish workaround I ended up going with. Not truly equivalent, but works for my purposes.
# "abstract" base class
class MyBaseClass( Exception ):
def __init__(self):
if not hasattr(self, 'foo'):
raise NotImplementedError("Please implement abstract property foo")
class MyConcreteClass( MyBaseClass ):
pass
if __name__=='__main__':
a = MyConcreteClass()
print "We never reach here, which is good."