{"id":19358,"date":"2022-09-27T05:06:35","date_gmt":"2022-09-27T12:06:35","guid":{"rendered":"https:\/\/coderpad.io\/?p=19358"},"modified":"2023-06-05T14:04:19","modified_gmt":"2023-06-05T21:04:19","slug":"classes-object-oriented-programming-python","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/classes-object-oriented-programming-python\/","title":{"rendered":"Guide to Classes and Object-Oriented Programming in Python"},"content":{"rendered":"\n<p>Object-oriented programming (OOP) is a popular programming paradigm for writing software. Before the development of object-oriented programming, procedural programming was an efficient way of building or writing software.&nbsp;<\/p>\n\n\n\n<p>In procedural programming, programs are divided into interdependent functions, which is a straightforward process. However, due to the interdependence of functions, a change in one function can break several other functions.&nbsp;<\/p>\n\n\n\n<p>Object-oriented programming was developed to solve this problem.<\/p>\n\n\n\n<p>As a software developer, object-oriented programming will give you a deeper understanding of your code by learning what happens line by line and the ideas underlying it.<\/p>\n\n\n\n<p>In this tutorial, we will guide you through:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Understanding the concepts of object-oriented programming in Python<\/li><li>How to create a class and instantiate objects in Python.<\/li><li>The object-oriented programming pillars in Python<\/li><\/ul>\n\n\n\n<p>Let\u2019s dive in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Object-Oriented Programming (OOP) in Python?<\/strong><\/h2>\n\n\n\n<p>Object-oriented programming is a programming model that provides a method of structuring data using objects and classes. These objects represent real-world situations and events. The goal of object-oriented programming is to combine properties and methods that work together as an individual unit.<\/p>\n\n\n\n<p>For example, think of a car as an object with properties like brand name, model, color, size, etc. A car also has methods such as movement, speed, etc. These properties and methods are structured into an individual object or unit. This process of structuring is known as <em>object-oriented programming<\/em>.<\/p>\n\n\n\n<p>Python is an object-oriented programming language that enables developers to create applications primarily focusing on code reusability. In Python, everything is an object consisting of methods and properties, making it easy for you to build classes and objects.<\/p>\n\n\n\n<p>The main idea of object-oriented programming in Python is not just for data representation but to determine how the program is organized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Classes and objects in Python<\/strong><\/h2>\n\n\n\n<p>Simply put, a class is a blueprint or prototype for creating objects. To create an object, we need a class.<\/p>\n\n\n\n<p>A class is a template that defines a collection of attributes and methods that specify the behavior that an object can carry out with its data. For instance, let us use a book as a class. A book has properties that contain the details of a book, such as a title, author, year of publication, etc. The book itself is the object.&nbsp;<\/p>\n\n\n\n<p>Once we have a defined set of properties, our class becomes an object. It allows the creation of as many objects as required using the same class. Let\u2019s define a class to get a better understanding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Defining a class in Python<\/strong><\/h3>\n\n\n\n<p>A class is defined using the <code>class<\/code> keyword, followed by the class name, which is usually a word describing the class.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u2139\ufe0f Class names in Python are in camel case. Each word starts with a capital letter.<\/p><\/blockquote>\n\n\n\n<p>In this section, we will define a book class that stores data about a book.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 \u2026<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code, we created a book class using the <code>class<\/code> keyword. Within this class is where we will define our attributes. Subsequently, we will add a body using custom methods, but before that, the placeholder lets us run this code without throwing an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Instantiate an object in Python<\/strong><\/h3>\n\n\n\n<p>While a class is a sketch or blueprint of an object, an object is a set of attributes and functions built on real-world data. The process of creating an object in Python is called <em>instantiation<\/em>. An object is instantiated by adding the name of the class, Book, followed by an opening and closing bracket.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">&gt;&gt;&gt; Book(<span class=\"hljs-string\"><\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>These book examples are basic and not reflective yet of real-life applications. Before we get into more practical applications, we will first need to understand what <strong><em>dunder methods<\/em><\/strong><strong> <\/strong>are and what they do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating classes and objects with dunder methods<\/strong><\/h2>\n\n\n\n<p><em>Dunder <\/em>stands for \u201cdouble underscores.\u201d Methods that begin and end with double underscores are known as <em>dunder methods<\/em>.&nbsp;<\/p>\n\n\n\n<p>Dunder methods provide the functionality of a class when a specific event occurs in a program. Multiple dunder methods are used when working with classes and objects in Python. This article will focus on the <code><strong>init<\/strong>()<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Init method<\/strong><\/h3>\n\n\n\n<p>The &nbsp;<code>__init__()<\/code> method is also known as a <strong>constructor<\/strong>. It is a unique Python method that is automatically executed whenever a class is used to instantiate an object. We use the &nbsp;<code>__init__()<\/code> method when adding data or attributes to a new <code>class<\/code> object.<\/p>\n\n\n\n<p>When defining the <code><code>__init__()<\/code><\/code> method, we are to add some parameters such as <code>self<\/code> and other parameters like <code>name<\/code>, <code>title<\/code>, <code>age<\/code>, etc., depending on the attributes.<\/p>\n\n\n\n<p>Let us create a class and a set of objects using the &nbsp;<code><code>__init__()<\/code><\/code> method. Several common attributes of a book can be added, such as title, author, and year of publication:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2013<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>)\nprint(book1.title)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">&gt;&gt;&gt; Americanah<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, we created a <code>Book<\/code> class with an <code><code>__init__()<\/code><\/code> function that initializes three parameters: <code>title<\/code>, <code>author<\/code>, and <code>year<\/code>.<\/p>\n\n\n\n<p>Next, we passed three parameters, although there are four arguments in the <code><code>__init__()<\/code><\/code>method. This is because the <code>self<\/code> parameter is only required when defining a method. It is not necessary to include it when calling the method.<\/p>\n\n\n\n<p>Also, conventionally, the <code>self<\/code> parameter must come before other parameters. Python subsequently uses this parameter to create the <code>Book<\/code> instance.<\/p>\n\n\n\n<p>Lastly, we instantiated a new object by adding values to the object. You can access any of the instance attributes of the <code>Book<\/code> instance using the dot notation.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u2139\ufe0f Attributes are variables that can be accessed using instances. Example: <code>book1.title<\/code> .<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>The self parameter<\/strong><\/h3>\n\n\n\n<p>The <code>self<\/code> parameter is a representation of the class instance. This parameter allows you to access the attributes in a class. In Python, it is a convention that the<code>self<\/code> parameter is to be provided first before other parameters in the instance method; otherwise, it will result in an error.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">\"This is a bookstore\"<\/span>)\nobject = Book()\nprint(<span class=\"hljs-string\">\"Welcome\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Traceback (<span class=\"hljs-string\">most recent call last):\n\u00a0 File \"\/Users\/anitaachu\/character-encoding\/app.py\", line 5, in &lt;module&gt;\n\u00a0 \u00a0 object = Book(<\/span>)\nTypeError: __init__(<span class=\"hljs-string\">) takes 0 positional arguments but 1 was given<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\"><p>\u2139\ufe0f The <code>self<\/code> parameter is not a Python keyword. Therefore, you can use any other word instead of &#8220;self.&#8221; However, it is good programming practice to use <code>self<\/code>.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Modifying and deleting objects<\/strong><\/h2>\n\n\n\n<p>Imagine you had to review the books in your store, and you realized one of the books had the wrong year of publication. You can easily modify the attributes of objects in the <code>Book<\/code> class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0\n\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2016<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>)\n\n<span class=\"hljs-comment\"># modifying attribute<\/span>\nbook1.year = <span class=\"hljs-number\">2013<\/span>\n\nprint(book1.year)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the code is executed, you will have this result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">&gt;&gt;&gt; <span class=\"hljs-number\">2013<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can also delete object values or the object itself using the <code>del<\/code> method.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0\n\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2016<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>)\n\n<span class=\"hljs-comment\"># deleting attribute<\/span>\n<span class=\"hljs-keyword\">del<\/span> book2.year\n\nprint(book2.year)\n\nWhen the code <span class=\"hljs-keyword\">is<\/span> executed, you will have this result:\nTraceback (most recent call last):\n\u00a0 File <span class=\"hljs-string\">\"\/Users\/anitaachu\/character-encoding\/app.py\"<\/span>, line <span class=\"hljs-number\">15<\/span>, <span class=\"hljs-keyword\">in<\/span> &lt;module&gt;\n\u00a0 \u00a0 print(book2.year)\nAttributeError: <span class=\"hljs-string\">'Book'<\/span> object has no attribute <span class=\"hljs-string\">'year'<\/span>\u00a0<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><strong>Object-oriented programming principles<\/strong><\/h2>\n\n\n\n<p>The primary object-oriented programming principles include:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Encapsulation<\/li><li>Inheritance<\/li><li>Polymorphism<\/li><\/ol>\n\n\n\n<p>These principles are referred to as the pillars of object-oriented programming. Each concept is a broad topic and would take several articles to explore thoroughly. However, this article will discuss the basics of these concepts to give you a good understanding of what they are.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Encapsulation<\/strong><\/h2>\n\n\n\n<p>Encapsulation is the process of restricting users\u2019 access to specific class attributes or methods by only allowing the attributes to be available via particular methods. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u2139\ufe0f Encapsulation is often referred to as <em>data hiding<\/em>. <\/p><\/blockquote>\n\n\n\n<p>With encapsulation, you can only share or expose what is needed for the user. The purpose of encapsulation is to prevent unauthorized modification of data.<\/p>\n\n\n\n<p>In encapsulation, object attributes are made private and can only be modified by the object method. These objects\u2019 attributes are known as <em>private attributes.<\/em><\/p>\n\n\n\n<p><strong>How encapsulation works<\/strong><\/p>\n\n\n\n<p>The data contained in an object and the methods that manipulate the data are wrapped together in one unit. This restricts the variables that can be accessed.<\/p>\n\n\n\n<p>For example, imagine you&#8217;re working with an organization that has salespeople and admins. The admins have access to all records of sales and finances. In a situation where a salesperson needs access to sales records, they cannot access this data without contacting an admin and requesting access to the sales record. This is <strong>encapsulation<\/strong>.<\/p>\n\n\n\n<p>In Python, we use getters and setters to implement encapsulation. The getters and setters are methods that give us access to and modify the values of the private attributes, respectively.<\/p>\n\n\n\n<p>In Python, we use double underscore <code>__<\/code> to declare a private attribute. Let\u2019s add a private attribute to the <code>Book<\/code> class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># public attributes<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># private attribute<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year: <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.price}<\/span>\"<\/span>\n\u00a0 \u00a0\n\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2016<\/span>, <span class=\"hljs-number\">6000<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>, <span class=\"hljs-number\">5000<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>, <span class=\"hljs-number\">4000<\/span>)\n\nprint(book3.title)\nprint(book3.author)\nprint(book3.year)\nprint(book3.__price)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Stay with me\nAyobami Adebayo\n<span class=\"hljs-number\">2017<\/span>\nTraceback (<span class=\"hljs-string\">most recent call last):\n\u00a0 File \"\/Users\/anitaachu\/character-encoding\/app.py\", line 26, in &lt;module&gt;\n\u00a0 \u00a0 print(book3.__price<\/span>)\nAttributeError: <span class=\"hljs-string\">'Book'<\/span> object has no attribute <span class=\"hljs-string\">'__price'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, <code>price<\/code> is a private attribute. Since it cannot be accessed outside the class, the output prints all the attributes <em>except<\/em> the <code>price<\/code> attribute.<\/p>\n\n\n\n<p>However, we can access the private attribute using getter and setter methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-comment\"># constructor<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># public data<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># private attribute<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-comment\"># getter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_price<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> self.__price\n\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># setter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_price<\/span><span class=\"hljs-params\">(self, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year: <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.get_price()}<\/span>\"<\/span>\n\u00a0 \u00a0\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2016<\/span>, <span class=\"hljs-number\">6000<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>, <span class=\"hljs-number\">5000<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>, <span class=\"hljs-number\">4000<\/span>)\n\n<span class=\"hljs-comment\"># modifying price using setter<\/span>\nbook2.set_price(<span class=\"hljs-number\">4000<\/span>)\nprint(book2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Book: Teller of secrets, Author: Bisi Adjapon, Year: <span class=\"hljs-number\">2021<\/span>, Price: <span class=\"hljs-number\">4000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Bravo! We got the private attribute and the modified price of <code>book2<\/code>.<\/p>\n\n\n\n<p>However, bear in mind that these private attributes are not really protected. For example, we can access the <code>price<\/code> attribute using <code>book1._Book__price<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-comment\"># constructor<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># public data<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year: <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.get_price()}<\/span>\"<\/span>\n\u00a0 \u00a0\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nbook1 = Book(<span class=\"hljs-string\">\"Americanah\"<\/span>, <span class=\"hljs-string\">\" Chimamanda Adichie\"<\/span>, <span class=\"hljs-number\">2016<\/span>, <span class=\"hljs-number\">6000<\/span>)\nbook2 = Book(<span class=\"hljs-string\">\"Teller of secrets\"<\/span>, <span class=\"hljs-string\">\"Bisi Adjapon\"<\/span>, <span class=\"hljs-number\">2021<\/span>, <span class=\"hljs-number\">5000<\/span>)\nbook3 = Book(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>, <span class=\"hljs-number\">4000<\/span>)\n\n<span class=\"hljs-comment\"># modifying price using setter<\/span>\nbook1._Book__price\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># private attribute<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-comment\"># getter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_price<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> self.__price\n\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># setter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_price<\/span><span class=\"hljs-params\">(self, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Book: Teller of secrets, Author: Bisi Adjapon, Year: <span class=\"hljs-number\">2021<\/span>, Price: <span class=\"hljs-number\">4000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><strong>Inheritance<\/strong><\/h2>\n\n\n\n<p>Inheritance is considered the most fundamental principle of object-oriented programming. Inheritance refers to the ability of one class to derive its methods and attributes from another class.&nbsp;<\/p>\n\n\n\n<p>In this case, the class that derives the attribute is known as the <em>child class<\/em>, while the class from which the child class derives its attributes is known as the <em>parent class<\/em>. The primary purpose of inheritance is to aid the reusability of code.&nbsp;<\/p>\n\n\n\n<p>The child class inherits all the attributes and functions of the parent class. Therefore, we do not have to write the same lines of code repeatedly. We can also add more features without changing the class.<\/p>\n\n\n\n<p><strong>How inheritance works<\/strong><\/p>\n\n\n\n<p>To demonstrate, we\u2019ll use our book example. A book is a class with common attributes like title, author, price, and year of publication. This holds true for both fiction and nonfiction books &#8212; which means that both types of books will inherit those particular attributes.<\/p>\n\n\n\n<p>Using the bookstore example, we can add a <code>Fiction<\/code> and a <code>Nonfiction<\/code> class, which will inherit the <code>title<\/code>, <code>author<\/code>, <code>year<\/code>, and <code>price<\/code> attributes.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># public data<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># private attribute<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-comment\"># getter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_price<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> self.__price\n\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># setter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_price<\/span><span class=\"hljs-params\">(self, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year: <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.get_price()}<\/span>\"<\/span>\n\u00a0 \u00a0\n<span class=\"hljs-comment\"># child class<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Fiction<\/span><span class=\"hljs-params\">(Book)<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 super().__init__(title, author, year, price)\n\n<span class=\"hljs-comment\"># child class<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Non_fiction<\/span><span class=\"hljs-params\">(Book)<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 super().__init__(title, author, year, price)\n\n<span class=\"hljs-comment\"># instantiating a new object<\/span>\nfiction1 = Fiction(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>, <span class=\"hljs-number\">4000<\/span>)\nnon_fiction1 = Non_fiction(<span class=\"hljs-string\">\"There Was a Country\"<\/span>, <span class=\"hljs-string\">\"Chinua Achebe\"<\/span>, <span class=\"hljs-number\">2012<\/span>, <span class=\"hljs-number\">6000<\/span>)\n\nprint(fiction1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you run your terminal, you should get the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Book: Stay with me, Author: Ayobami Adebayo, Year: <span class=\"hljs-number\">2017<\/span>, Price: <span class=\"hljs-number\">4000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><strong>Polymorphism<\/strong><\/h2>\n\n\n\n<p>Polymorphism is another fundamental concept in Python programming. In literal terms, polymorphism refers to the occurrence of a similar thing in various forms. Polymorphism in Python refers to the ability of an object, class, or method to take different forms at different instances. A subclass, also known as a child class, can adopt a method of its parent class through polymorphism.<\/p>\n\n\n\n<p><strong>How polymorphism works<\/strong><\/p>\n\n\n\n<p>In polymorphism, classes can implement inherited methods in different ways. This enables us to define methods in a subclass with the same name as in its parent class. In some situations, the subclass may modify its parent class&#8217;s method where the parent class&#8217;s method does not fit. In this case, the method must be reimplemented in the child class.<\/p>\n\n\n\n<p>For a better understanding, let us work with the bookstore example again:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Book<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># public data<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.title =\u00a0 title\n\u00a0 \u00a0 \u00a0 \u00a0 self.author = author\n\u00a0 \u00a0 \u00a0 \u00a0 self.year = year\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># private attribute<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-comment\"># getter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_price<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> self.__price\n\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># setter method<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_price<\/span><span class=\"hljs-params\">(self, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.__price = price\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year: <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.get_price()}<\/span>\"<\/span>\n\u00a0 \u00a0\n<span class=\"hljs-comment\"># child class<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Fiction<\/span><span class=\"hljs-params\">(Book)<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, title, author, year, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 super().__init__(title, author, year, price)\n\n\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Book: <span class=\"hljs-subst\">{self.title}<\/span>, Author: <span class=\"hljs-subst\">{self.author}<\/span>, Year:\u00a0 <span class=\"hljs-subst\">{self.year}<\/span>, Price: <span class=\"hljs-subst\">{self.get_price()}<\/span>\"<\/span>\n\n<span class=\"hljs-comment\"># instantiating a new object\u00a0<\/span>\nfiction1 = Fiction(<span class=\"hljs-string\">\"Stay with me\"<\/span>, <span class=\"hljs-string\">\"Ayobami Adebayo\"<\/span>, <span class=\"hljs-number\">2017<\/span>, <span class=\"hljs-number\">4000<\/span>)\n\nprint(fiction1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python profile\" data-shcb-language-slug=\"profile\"><span><code class=\"hljs language-profile shcb-wrap-lines\">Book: Stay with me, Author: Ayobami Adebayo, Year: <span class=\"hljs-number\">2017<\/span>, Price: <span class=\"hljs-number\">4000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python profile<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">profile<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, the parent class, <code>Book<\/code>, contains the <code>repr<\/code> method, which returns the string representation of an object. The <code>Fiction<\/code> subclass also uses this method; it is invoked whenever an object is printed.<\/p>\n\n\n\n<p>In addition, when the child class inherits a method from the parent class, polymorphism allows us to modify the method in the child class to fit its needs. This process of modification is known as the <em>overriding method<\/em>.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MobileDevice<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, color, price)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 self.name = name\n\u00a0 \u00a0 \u00a0 \u00a0 self.color = color\n\u00a0 \u00a0 \u00a0 \u00a0 self.price = price\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">details<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">f\"Details: <span class=\"hljs-subst\">{self.name}<\/span>, Color: <span class=\"hljs-subst\">{self.color}<\/span>, Price: <span class=\"hljs-subst\">{self.price}<\/span>\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">battery<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">\"Mobile device has one battery\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">camera<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">\"Mobile phones have dual camera\"<\/span>)\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Iphone<\/span><span class=\"hljs-params\">(Mobile_device)<\/span>:<\/span>\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">details<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">f\"Details: <span class=\"hljs-subst\">{self.name}<\/span>, Color: <span class=\"hljs-subst\">{self.color}<\/span>, Price: <span class=\"hljs-subst\">{self.price}<\/span>\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">battery<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">\"Mobile device has one battery\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">camera<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 print(<span class=\"hljs-string\">\"Mobile device has three cameras\"<\/span>)\n\u00a0 \u00a0\n\nmobile_device = Mobile_device(<span class=\"hljs-string\">'Pixel 6'<\/span>,<span class=\"hljs-string\">'Gold'<\/span>, <span class=\"hljs-number\">50000<\/span> )\nmobile_device.details()\nmobile_device.battery()\nmobile_device.camera()\n\nios = Iphone(<span class=\"hljs-string\">'iPhone 13 Pro'<\/span>,<span class=\"hljs-string\">'Green'<\/span>, <span class=\"hljs-number\">65000<\/span>)\nios.details()\nios.battery()\nios.camera()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">Details: Pixel <span class=\"hljs-number\">6<\/span>, Color: Gold, Price: <span class=\"hljs-number\">50000<\/span>\nMobile device has one battery\nMobile phones have dual camera\n\nDetails: iPhone <span class=\"hljs-number\">13<\/span> Pro, Color: Green, Price: <span class=\"hljs-number\">65000<\/span>\nMobile device has one battery\nMobile device has three cameras<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As a result of polymorphism, the child class (<code>Iphone<\/code>) overrides the <code>camera()<\/code> method inherited from its parent class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Wrapping up<\/strong><\/h2>\n\n\n\n<p>In this tutorial, we introduced the concept of objects and classes in Python and the fundamental principles of object-oriented programming.<\/p>\n\n\n\n<p>These concepts do not only apply to Python. Object-oriented programming applies to most programming languages, such as Java, C#, C++, JavaScript, etc. However, their implementation differs according to the language.<\/p>\n\n\n\n<p>I hope this tutorial was of help to you.<\/p>\n\n\n\n<p>Happy Coding! \ud83d\ude42<\/p>\n\n\n\n<p><em>Anita Achu is a software developer and technical writer passionate about creating technical content to aid developers to learn about software development tools and how to use these tools in building and providing solutions to problems.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object-oriented programming is a popular programming model that provides a method of structuring data using objects and classes. This article provides a great introduction to OOP in Python.<\/p>\n","protected":false},"author":12,"featured_media":19531,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[37],"keyword-cluster":[],"class_list":["post-19358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/19358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=19358"}],"version-history":[{"count":138,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/19358\/revisions"}],"predecessor-version":[{"id":21048,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/19358\/revisions\/21048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/19531"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=19358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=19358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=19358"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=19358"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=19358"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=19358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}