Skip to content

Instantly share code, notes, and snippets.

@humamfauzi
Created April 17, 2021 07:51
Show Gist options
  • Save humamfauzi/cc3e149227d0801f7969ec1a66850d8b to your computer and use it in GitHub Desktop.
Save humamfauzi/cc3e149227d0801f7969ec1a66850d8b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Python Abstract Class\n\nPython is a semi OOP language that has several OOP feature. While python cannot enforce interface of a class, it can create an abstract class to enforce rigidity while building a subclass. In this note, we will explore the ABC (Abstract Base Class) for python,","metadata":{}},{"cell_type":"code","source":"from abc import ABC, abstractmethod","metadata":{"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"markdown","source":"ABC stands for abstract base class, any class that we want to enforce using abstract class should inherit from ABC. `abstractmethod` is a decorator for a class method that the function will become apparent soon","metadata":{}},{"cell_type":"code","source":"class AbstractExample(ABC):\n def __init__(self, init_val):\n self.init_val = init_val\n \n @abstractmethod\n def action(self):\n pass\n \nclass ConcreteExample(AbstractExample):\n def __init__(self, init_val):\n super().__init__(init_val)\n\nce = ConcreteExample(22)","metadata":{"trusted":true},"execution_count":9,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-9-48ab858026a4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minit_val\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mce\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mConcreteExample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m22\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class ConcreteExample with abstract methods action"],"ename":"TypeError","evalue":"Can't instantiate abstract class ConcreteExample with abstract methods action","output_type":"error"}]},{"cell_type":"markdown","source":"Decorator `@abstractmethod` enforce subclass `ConcreteExample` to have `.action` method. Let see if we give `ConcreteExample` an action method.","metadata":{}},{"cell_type":"code","source":"class ConcereteExampleWithMethod(AbstractExample):\n def __init__(self, init_val):\n super().__init__(init_val)\n \n def action(self):\n print(self.init_val)\n \ncewm = ConcereteExampleWithMethod(22)\ncewm.action()","metadata":{"trusted":true},"execution_count":10,"outputs":[{"name":"stdout","text":"22\n","output_type":"stream"}]},{"cell_type":"markdown","source":"As we can see, we succesfully initiate `ConcreteExampleWithMethod` class when inheriting from `AbstractExample`. Any abstract class should inherit from ABC otherwise the decorator abstract method wont be enforced. ","metadata":{}},{"cell_type":"code","source":"class AbstractExample(ABC):\n def __init__(self, init_val):\n self.init_val = init_val\n \n @abstractmethod\n def addition(self, a, b):\n return a + b\n \nclass ConcreteExample(AbstractExample):\n def __init__(self, init_val):\n super().__init__(init_val)\n \n def addition(self, a, b, c):\n return super().addition(a, b) + c\n \nce = ConcreteExample(22)\nce.addition(1, 2, 3)","metadata":{"trusted":true},"execution_count":39,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-39-9639027b2a24>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# return super().addition(a, b) + c\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mce\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mConcreteExample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m22\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0mce\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maddition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class ConcreteExample with abstract methods addition"],"ename":"TypeError","evalue":"Can't instantiate abstract class ConcreteExample with abstract methods addition","output_type":"error"}]},{"cell_type":"markdown","source":"","metadata":{}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment