{"id":1056,"date":"2016-12-30T14:34:01","date_gmt":"2016-12-30T14:34:01","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2016\/12\/30\/python-what-are-abstract-classes\/"},"modified":"2021-07-31T19:24:57","modified_gmt":"2021-07-31T19:24:57","slug":"python-what-are-abstract-classes","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html","title":{"rendered":"Python – What are Abstract Classes?"},"content":{"rendered":"
An abstract class can be considered a blueprint for other classes, allowing you to mandate a set of methods that must be created within any child classes built from your abstract class.<\/p>\n
Lets first look at how you create an abstract class. First we import abc<\/span>, we define the class as a metaclass using the __metaclass__<\/span> magic attribute, we then decorate using @abc.abstractmethod<\/span>.<\/p>\n Lets look at an abstract class in action.<\/p>\n First, if we go and build a child class from this base class, using the correct methods i.e abstract methods, we should see no problems. Like so,<\/p>\n >>> m = MyClass() However if we create a child class with methods different to what was set within our abstract class, the class will not instantiate. >>> m = MyClass() And finally there is one last point that I should highlight. Due to the fact that an abstract class is not an concrete class, it cannot be instantiated. Heres an example,<\/p>\n An abstract class can be considered a blueprint for other classes, allowing you to mandate a set of methods that must be created within any child classes built from your abstract class. Creation Lets first look at how you create an abstract class. First we import abc, we define the class as a metaclass using … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":1055,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\nimport abc\r\n\r\nclass TestClass(object):\r\n __metaclass__ = abc.ABCMeta\r\n\r\n @abc.abstractmethod\r\n def set_total(self,input):\r\n \"\"\"Set a value for instance.\"\"\"\r\n return\r\n\r\n @abc.abstractmethod\r\n def get_total(self):\r\n \"\"\"Get and return a value for instance.\"\"\"\r\n return<\/pre>\n
Subclassing<\/h2>\n
class MyClass(TestClass):\r\n def set_total(self,x):\r\n self.total = x\r\n\r\n def get_total(self):\r\n return self.total\r\n\r\n>>> m = MyClass()\r\n>>> print m\r\n<__main__.MyClass object at 0x100414910><\/pre>\n
\n>>> print m
\n<__main__.MyClass object at 0x100414910><\/p>\n
\nNotice how I have changed the name of the get_total<\/span> method to xyz_get_total<\/span><\/p>\nclass MyClass(TestClass):\r\n def set_total(self,x):\r\n self.total = x\r\n\r\n def xyz_get_total(self):\r\n return self.total\r\n\r\n>>> m = MyClass()\r\nTraceback (most recent call last):\r\nFile \"<stdin>\", line 1, in <module>\r\nTypeError: Can't instantiate abstract class MyClass with abstract methods get_total<\/pre>\n
\nTraceback (most recent call last):
\nFile “<stdin>”, line 1, in <module>
\nTypeError: Can’t instantiate abstract class MyClass with abstract methods get_total<\/p>\nAbstract Class Instantiation<\/h2>\n
>>> t = TestClass()\r\nTraceback (most recent call last):\r\n File \"\", line 1, in \r\nTypeError: Can't instantiate abstract class TestClass with abstract methods get_total, set_total<\/pre>\n","protected":false},"excerpt":{"rendered":"