def test__abstractclass(self):
@six.add_metaclass(ABCMeta)
class Something(object):
@abstractmethod
def __init__(self, x=10):
self.x = x
@abstractclass
class SomethingElse(Something):
# derived classes from this class make use
# of this class __init__
# since this __init__ overrides the parent's
# @abstractmethod instances could be created,
# by making the class abstract with @abstractclass
# this can't be done, derived classes can
# ... that is the goal
def __init__(self, x=10, y=20):
super(SomethingElse, self).__init__(x)
self.y = y
class ABCDerived(SomethingElse):
pass
with self.assertRaises(TypeError):
Something(x=20)
with self.assertRaises(TypeError):
SomethingElse(x=20, y=30)
x = 20
y = 30
abcDerived = ABCDerived(x, y)
self.assertEqual(abcDerived.x + abcDerived.y, x+y)
评论列表
文章目录