{"id":3038,"date":"2020-07-31T05:02:38","date_gmt":"2020-07-31T05:02:38","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3038"},"modified":"2020-07-31T08:31:14","modified_gmt":"2020-07-31T08:31:14","slug":"class-composition-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/class-composition-in-python\/","title":{"rendered":"Class Composition in Python"},"content":{"rendered":"<h2>Introduction to Class composition<\/h2>\n<p>Class composition is an alternative to class inheritance. It is much more useful than the latter. It reduces the complexity of code and makes it much easier to understand. Consider this code :<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/code01.png\" alt=\"code01\"\/><\/p>\n<h3>Analyzing the approach<\/h3>\n<p>This makes little sense as we can see, in this code, class \u2018Book\u2019 inherits from the class \u2018Bookshelf\u2019. The idea being, a book is contained inside the bookshelf. Here we need to use the<em>__str__<\/em> method inside the Book class in order to avoid printing \u201c<em>Bookshelf with 150 books<\/em>\u201d and print \u201c<em>Book 5 Second Rule<\/em>\u201d when printing an object of Book class. While this may work, its a bad approach and this is for two reasons:-<\/p>\n<ol>\n<li><strong>Conceptual issue<\/strong> &#8211;  The conceptual reason is that when we do inheritance, we are essentially treating it as an evolutionary inheritance. We&#8217;re saying that a book is a bookshelf and something more. For example, akin to the fact that all tigers are mammals, but not all mammals are tigers. So all books are bookshelves, but not all bookshelves are books. The bookshelf can still be used on its own. That breaks down when we talk about books and bookshelves because a book is something completely different from a bookshelf. A bookshelf can contain books, but one isn&#8217;t the other.<\/li>\n<li><strong>Technical issue<\/strong> &#8211;  The technical reason why this breaks down is that we&#8217;ve got this book class that inherits from the bookshelf, but actually we are not using inside it anything about the bookshelf. So we are completely overriding the <em>__str__<\/em> method because we don&#8217;t want anything to do with bookshelves in there. We actually don&#8217;t need these at all because there is no point in <strong>setting the quantity<\/strong> if we&#8217;re not going to use it in the methods.<\/li>\n<\/ol>\n<p>So the conceptual reason is that a book is not a bookshelf and the technical reason is that there is no reason to inherit if we don\u2019t use the inheritance anywhere. So this is where the composition comes in.<br \/>\nInstead of setting the number of books in the bookshelf, we can allow the constructor to take in a number of books. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/code02.png\" alt=\"code02\" \/><br \/>\n<br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/code03.png\" alt=\"code03\"\/><\/p>\n<p>Here, we have removed the super method and the quantity parameter. Updated the <em>__str__<\/em> method in the Bookshelf class to find the length of the books tuple(*self.books) which can contain N number of books. It is used for passing an arbitrary number of arguments. We create an object of Bookshelf class called \u2018shelf\u2019, pass in the books and print it which will return the <em>__str__<\/em> method in the bookshelf class. As you can see our code has become much simpler, cleaner and it allows us to add several books to the bookshelf.<\/p>\n<p><small><em>Get certification for your knowledge in the fundamentals of Computer functioning by clearing the <a href=\"https:\/\/www.studysection.com\/computer-fundamentals-advanced\">Computer Certification exam<\/a> conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Class composition Class composition is an alternative to class inheritance. It is much more useful than the latter.<\/p>\n","protected":false},"author":1,"featured_media":3042,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[505,504,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Class Composition in Python Programming<\/title>\n<meta name=\"description\" content=\"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Class Composition in Python Programming\" \/>\n<meta property=\"og:description\" content=\"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-31T05:02:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-31T08:31:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/python-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\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:\/\/studysection.com\/blog\/class-composition-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Class Composition in Python\",\"datePublished\":\"2020-07-31T05:02:38+00:00\",\"dateModified\":\"2020-07-31T08:31:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\"},\"wordCount\":490,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Class\",\"Composition\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/class-composition-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\",\"name\":\"StudySection Blog - Class Composition in Python Programming\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-07-31T05:02:38+00:00\",\"dateModified\":\"2020-07-31T08:31:14+00:00\",\"description\":\"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/class-composition-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/class-composition-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Class Composition in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Class Composition in Python Programming","description":"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.","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:\/\/studysection.com\/blog\/class-composition-in-python\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Class Composition in Python Programming","og_description":"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.","og_url":"https:\/\/studysection.com\/blog\/class-composition-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-07-31T05:02:38+00:00","article_modified_time":"2020-07-31T08:31:14+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/python-1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Class Composition in Python","datePublished":"2020-07-31T05:02:38+00:00","dateModified":"2020-07-31T08:31:14+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/"},"wordCount":490,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Class","Composition","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/class-composition-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/","url":"https:\/\/studysection.com\/blog\/class-composition-in-python\/","name":"StudySection Blog - Class Composition in Python Programming","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-07-31T05:02:38+00:00","dateModified":"2020-07-31T08:31:14+00:00","description":"Class composition is an alternative to class inheritance. It reduces the complexity of code and makes it much easier to understand.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/class-composition-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/class-composition-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Class Composition in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":2113,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3038"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3038"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3038\/revisions"}],"predecessor-version":[{"id":3044,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3038\/revisions\/3044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3042"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}