[lang-ref] ( abstract_method ) ( python )
def test_abstract_method():
# use abstractmethod
from abc import ABC, abstractmethod
class Parent(ABC): # <-- inherit from ABC
@abstractmethod # <-- mark as an abstractmethod
def method01(self):
pass
def method02(self):
return 'parent method02'
class Child01(Parent):
pass
class Child02(Parent):
def method01(self):
return 'child method01'
with pytest.raises(TypeError):
Child01() # <--- can't instantiate because method01() is not implemented.
c = Child02()
assert c.method01() == 'child method01'
assert c.method02() == 'parent method02'
# Note: 'abc' is not an arbitrary name; it's short for "Abstract Base Classes".