Zaimplementuj metaklasę sprawdzającą implementację interfejsu. Nazwy metod powinny być przechowywane w atrybucie __interfaces__
In [ ]:
class Interface(type):
def __init__(cls, classname, bases, classdict):
super().__init__(classname, bases, classdict)
implements = set(dir(cls))
for interface_attributes, interface_class in ((set(dir(interface)), interface) for interface in cls.__interfaces__):
if not implements >= interface_attributes:
missing_attributes = str(interface_attributes - implements)
raise AttributeError("In {} is missing: {}".format(interface_class, missing_attributes))
class IPunkt():
x = 5
y = 10
def odleglosc(self):
pass
class Punkt(object, metaclass=Interface):
__interfaces__ = [ IPunkt]
x = 1
y = 2
def __init__(self, x, y):
self.x = x
self.y = y
def odleglosc(self):
pass
p = Punkt(1, 2)