{"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

Creation<\/h2>\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

import 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

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

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

>>> m = MyClass()
\n>>> print m
\n<__main__.MyClass object at 0x100414910><\/p>\n

However if we create a child class with methods different to what was set within our abstract class, the class will not instantiate.
\nNotice how I have changed the name of the get_total<\/span> method to xyz_get_total<\/span><\/p>\n

class 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

>>> m = MyClass()
\nTraceback (most recent call last):
\nFile “<stdin>”, line 1, in <module>
\nTypeError: Can’t instantiate abstract class MyClass with abstract methods get_total<\/p>\n

Abstract Class Instantiation<\/h2>\n

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

>>> 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":"

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":"\nPython - What are Abstract Classes? - Fir3net<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - What are Abstract Classes? - Fir3net\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-30T14:34:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-31T19:24:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"128\" \/>\n\t<meta property=\"og:image:height\" content=\"128\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"Python – What are Abstract Classes?\",\"datePublished\":\"2016-12-30T14:34:01+00:00\",\"dateModified\":\"2021-07-31T19:24:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\"},\"wordCount\":229,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\",\"url\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\",\"name\":\"Python - What are Abstract Classes? - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png\",\"datePublished\":\"2016-12-30T14:34:01+00:00\",\"dateModified\":\"2021-07-31T19:24:57+00:00\",\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png\",\"width\":128,\"height\":128},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming\",\"item\":\"https:\/\/www.fir3net.com\/programming\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\/\/www.fir3net.com\/programming\/python\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python – What are Abstract Classes?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - What are Abstract Classes? - Fir3net","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html","og_locale":"en_US","og_type":"article","og_title":"Python - What are Abstract Classes? - Fir3net","og_description":"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","og_url":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html","og_site_name":"Fir3net","article_published_time":"2016-12-30T14:34:01+00:00","article_modified_time":"2021-07-31T19:24:57+00:00","og_image":[{"width":128,"height":128,"url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png","type":"image\/jpeg"}],"author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"Python – What are Abstract Classes?","datePublished":"2016-12-30T14:34:01+00:00","dateModified":"2021-07-31T19:24:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html"},"wordCount":229,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"image":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png","articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html","url":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html","name":"Python - What are Abstract Classes? - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage"},"image":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png","datePublished":"2016-12-30T14:34:01+00:00","dateModified":"2021-07-31T19:24:57+00:00","description":"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","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#primaryimage","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/2016\/12\/images_articles_1483503505_web-design-3.png","width":128,"height":128},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/Programming\/Python\/python-what-are-abstract-classes.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Programming","item":"https:\/\/www.fir3net.com\/programming"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/www.fir3net.com\/programming\/python"},{"@type":"ListItem","position":4,"name":"Python – What are Abstract Classes?"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/1056"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":0,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media\/1055"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}