{"id":312960,"date":"2021-07-31T16:00:33","date_gmt":"2021-07-31T13:00:33","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-get-the-most-out-of-python-classes\/"},"modified":"2021-07-31T16:00:33","modified_gmt":"2021-07-31T13:00:33","slug":"how-to-get-the-most-out-of-python-classes","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/","title":{"rendered":"#How to get the most out of Python classes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e13be1fcf5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e13be1fcf5\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#Python_classes_the_very_basics\" >Python classes: the very basics<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#Classes_are_amazing_%E2%80%94_in_theory\" >Classes are amazing \u2014 in theory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#Inheritance_writing_the_DNA_of_a_data_structure\" >Inheritance: writing the DNA of a data structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#When_to_use_classes\" >When to use classes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#When_classes_are_a_bad_idea\" >When classes are a bad idea<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-the-most-out-of-python-classes\/#The_bottom_line_Python_classes_are_a_two-edged_sword\" >The bottom line: Python classes are a two-edged sword<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#How to get the most out of Python classes<\/strong>&#8221;<\/p>\n<div>There should only be one \u2014 and preferably only one \u2014 obvious way to do it\u201d, says the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.python.org\/dev\/peps\/pep-0020\/\">Zen of Python<\/a>. Yet there are areas where even seasoned programmers debate what the right or wrong way to do things is.<\/p>\n<p>One of these areas is Python classes. Borrowed from Object-Oriented Programming, they\u2019re quite beautiful constructs that you can expand and modify as you code.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/fast.wistia.net\/embed\/iframe\/xo23fcyip6?videoFoam=true&amp;autoPlay=true\" title=\"Whats it like to be a startup founder in Barcelona? Video\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"no\" class=\"wistia_embed\" name=\"wistia_embed\" allowfullscreen=\"\" msallowfullscreen=\"\" width=\"100%\" height=\"100%\"><\/iframe><\/p>\n<p>The big problem is that classes can make your code more complicated than necessary, and make it harder to read and maintain. So when should you use classes, and when should you use standard functions instead?<\/p>\n<p>This story is a deeper dive into the matter. So if you\u2019re in a hurry, you can skip the following two sections and scroll right down to the sections <em>When to use classes<\/em> and <em>When classes are a bad idea<\/em>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_classes_the_very_basics\"><\/span><strong>Python classes: the very basics<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Classes are objects that allow you to group data structures and procedures in one place. For example, imagine you\u2019re writing a piece of code to organize the inventory of a clothes shop.<\/p>\n<p>You could create a class that takes each item of clothing in the shop, and stores key quantities such as the type of clothing, and its color and size. We\u2019ll add an option to add a price, too.<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362455 aligncenter js-lazy\" alt=\"classes\" width=\"681\" height=\"148\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31.png 681w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-280x61.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-540x117.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-270x59.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362455 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31.png\" alt=\"classes\" width=\"681\" height=\"148\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31.png 681w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-280x61.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-540x117.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.31-270x59.png 270w\"\/><\/noscript><\/figure>\n<p><span>Now, we can define various instances of the class and keep them organized:<\/span><\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362456 aligncenter js-lazy\" alt=\"\" width=\"680\" height=\"73\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-280x30.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-540x58.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-270x29.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362456 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42.png\" alt=\"\" width=\"680\" height=\"73\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-280x30.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-540x58.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.46.42-270x29.png 270w\"\/><\/noscript><\/figure>\n<p>We would add these two lines without indent, after the definition of the class. This code will run, but it\u2019s not doing very much. We can add a method to set the price directly underneath the __init__ function, within the class definition:<\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362457 aligncenter js-lazy\" alt=\"\" width=\"676\" height=\"125\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03.png 676w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-270x50.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362457 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03.png\" alt=\"\" width=\"676\" height=\"125\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03.png 676w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-280x52.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-540x100.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.03-270x50.png 270w\"\/><\/noscript><\/figure>\n<p><span>We could also add some routines to tell us the price, or to promote an item by reducing the price:<\/span><\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362458 aligncenter js-lazy\" alt=\"\" width=\"677\" height=\"374\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-280x155.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-489x270.png 489w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-244x135.png 244w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362458 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12.png\" alt=\"\" width=\"677\" height=\"374\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-280x155.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-489x270.png 489w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.49.12-244x135.png 244w\"\/><\/noscript><\/figure>\n<p><span>Now, we can add some calls of our methods after the lines where we\u2019ve initialized the instances of the class:<\/span><\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362459 aligncenter js-lazy\" alt=\"\" width=\"679\" height=\"193\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-280x80.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-540x153.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-270x77.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362459 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43.png\" alt=\"\" width=\"679\" height=\"193\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43.png 679w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-280x80.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-540x153.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.50.43-270x77.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"9802\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">If you need to add more routines, you can just put them in the class definition.<\/p>\n<p id=\"91cd\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">The nicest part of all of this is that you can add and delete as many objects as you like. Deleting an attribute goes like so:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362460 aligncenter js-lazy\" alt=\"\" width=\"678\" height=\"59\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-540x47.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-270x23.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362460 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49.png\" alt=\"\" width=\"678\" height=\"59\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-540x47.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.49-270x23.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p data-selectable-paragraph=\"\"><span>And if you want to delete an entire object, you do like so:<\/span><\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362461 aligncenter js-lazy\" alt=\"\" width=\"677\" height=\"59\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-540x47.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-270x24.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362461 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55.png\" alt=\"\" width=\"677\" height=\"59\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55.png 677w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-540x47.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-10.52.55-270x24.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"8cab\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">All of this is neat, simple, and expandable. Try doing this implementation with standard functions, and you\u2019ll probably have a lot more trouble dealing with it.<\/p>\n<p id=\"acc0\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">From a theoretical point of view, there are more reasons why Python classes are a beautiful concept in many situations.<\/p>\n<figure class=\"nf ng nh ni nj js gd ge paragraph-image\">\n<div class=\"kf kg at kh v ki\" tabindex=\"0\" role=\"button\">\n<div class=\"gd ge ob\">\n<div class=\"og s at ns\">\n<div class=\"oh oi s\">\n<div class=\"dg oc ff fr fo mc v od oe of\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1362462 js-lazy\" alt=\"\" width=\"514\" height=\"684\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash.jpeg\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash.jpeg 700w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-158x210.jpeg 158w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-203x270.jpeg 203w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-101x135.jpeg 101w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-687x916.jpeg 687w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-414x552.jpeg 414w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-354x472.jpeg 354w\"\/><figcaption><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/thenextweb.com\/news\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Feditorial.thenextweb.com%2Fdd%2F2021%2F07%2F31%2Fhow-to-use-python-classes-syndication%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Classes are powerful concepts when used right. Photo by Muhannad Ajjan on Unsplash\" data-title=\"Share Classes are powerful concepts when used right. Photo by Muhannad Ajjan on Unsplash on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Classes are powerful concepts when used right. Photo by Muhannad Ajjan on Unsplash on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Classes are powerful concepts when used right. Photo by Muhannad Ajjan on Unsplash<\/figcaption><noscript><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1362462\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash.jpeg\" alt=\"\" width=\"514\" height=\"684\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash.jpeg 700w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-158x210.jpeg 158w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-203x270.jpeg 203w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-101x135.jpeg 101w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-687x916.jpeg 687w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-414x552.jpeg 414w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash-354x472.jpeg 354w\"\/><\/noscript><\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/figure>\n<h2 id=\"7e80\" class=\"me mf gx bf mg mh mi ku mj mk ml kx mm mn mo mp mq mr ms mt mu mv mw mx my mz hu\"><span class=\"ez-toc-section\" id=\"Classes_are_amazing_%E2%80%94_in_theory\"><\/span>Classes are amazing \u2014 in theory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"6c36\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Separation of concerns: giving every class its own job<\/strong><\/p>\n<p id=\"2795\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">If you\u2019ve attended lectures in computer <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a>, it\u2019s pretty likely that you\u2019ve stumbled across the principle of \u201cseparation of concerns\u201d. It basically means that you split up your program into different sections that deal with different pieces of information.<\/p>\n<p id=\"7a6d\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Classes, by their nature, allow you to keep to that principle. In other words, when you set out writing a program and you\u2019re thinking in terms of classes, you might be building a good architecture because you\u2019re ensuring that each problem has its own place.<\/p>\n<p><strong>Decoupling: making maintenance easier<\/strong><\/p>\n<p id=\"7632\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">Thinking in classes not only helps you keep features separate, but also independent of one another. Not only does this keep things neat and tidy; it is also a lot easier for maintenance.<\/p>\n<p id=\"2497\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Say you found a bug in one class: you could fix that bug without worrying about the other classes because there is no connection between them. Likewise, you could add new features without fearing that you\u2019ll get tangled up with other pieces of the software.<\/p>\n<p id=\"f683\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Implementation hiding: defining what programmers can and can\u2019t use<\/strong><\/p>\n<p id=\"136d\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">By using classes, you\u2019re ensuring that methods are only used on one set of data. This adds to the security of the code because you\u2019re less likely to use functions where they don\u2019t belong.<\/p>\n<p id=\"4a4d\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Encapsulation: changing the code but not the user experience<\/strong><\/p>\n<p id=\"0052\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">Storing data structures and methods together is also called encapsulation. Since all of this is hidden from the end user, this allows you to modify data structures and methods without compromising the user experience.<\/p>\n<p id=\"deee\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">For example, you might have to build a method that is quite complex. The advantage of encapsulation is that a user doesn\u2019t need to understand any of that complexity because they can use it like a black box.<\/p>\n<p id=\"76ee\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">It is completely possible to build black-box-functions without using classes. With classes, however, this type of functioning is practically ensured.<\/p>\n<h2 id=\"b593\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\" data-selectable-paragraph=\"\"><span class=\"ez-toc-section\" id=\"Inheritance_writing_the_DNA_of_a_data_structure\"><\/span>Inheritance: writing the DNA of a data structure<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"e51a\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">With classes, you only have to define a data structure once. When you define an instance of a class, that instance automatically inherits the given structure.<\/p>\n<p id=\"4823\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">In addition, inheritance makes it quite easy to delete or modify pieces of an instance or the whole class. This makes the whole construct more flexible.<\/p>\n<h2 id=\"960c\" class=\"me mf gx bf mg mh mi ku mj mk ml kx mm mn mo mp mq mr ms mt mu mv mw mx my mz hu\"><span class=\"ez-toc-section\" id=\"When_to_use_classes\"><\/span>When to use classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"aeeb\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">With so many advantages, it might be tempting to use a class for everything and anything. In practice, however, there are situations where using classes makes perfect sense, and others where it doesn\u2019t.<\/p>\n<p id=\"c603\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Bunching data and methods together<\/strong><\/p>\n<p id=\"9b25\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">As a rule of thumb, when you have a set of data with a specific structure and you want to perform specific methods on it, use a class. That is only valid, however, if you use multiple data structures in your code.<\/p>\n<p id=\"f3ef\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">If your whole code won\u2019t ever deal with more than one structure. If you only have one data structure, it really depends on the problem at hand. You can get a rough idea by sketching out your program with or without a class; usually you\u2019ll see pretty soon which solution is simpler.<\/p>\n<p id=\"4aae\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Beware of global variables<\/strong><\/p>\n<p id=\"ed67\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">Another rule of thumb is<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw kp\" href=\"https:\/\/www.reddit.com\/r\/learnpython\/comments\/6wsjt9\/i_still_dont_get_when_to_use_class_vs_functions\/\">this<\/a>: If you\u2019re tempted to use global variables to access data, it might be easier to define a class and build a method to access each piece of data.<\/p>\n<p id=\"d0c4\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Way easier than defining a new class!<\/p>\n<h2 id=\"e27f\" class=\"me mf gx bf mg mh mi ku mj mk ml kx mm mn mo mp mq mr ms mt mu mv mw mx my mz hu\"><span class=\"ez-toc-section\" id=\"When_classes_are_a_bad_idea\"><\/span>When classes are a bad idea<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>Use heapq for heaps<\/strong><\/p>\n<p>A heap, unlike a stack, is a way of storing data in a more flexible way because it has unlimited memory size and allows you to resize variables. On the other hand, accessing variables is slower with a heap and you must manage the memory yourself.<br \/>If a heap suits your purposes better, you don\u2019t need to define a class. Python\u2019s inbuilt heapq, or heap queue algorithm, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/python\/idiomatic-python-functions-versus-classes\/\">does the job for you<\/a>.<\/p>\n<p><strong>Consider using functools.partial()<\/strong><\/p>\n<p>You might be tempted to use a class because you\u2019re constantly calling a function with the same arguments. In most cases, it\u2019s a better idea to use functools.partial() instead.<\/p>\n<p>It\u2019s quite simple to implement. Say you have a function that multiplies two values, but you keep using it to double values. To avoid duplicate code, you could write this:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362464 aligncenter js-lazy\" alt=\"\" width=\"678\" height=\"184\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-280x76.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-540x147.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-270x73.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362464 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20.png\" alt=\"\" width=\"678\" height=\"184\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-280x76.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-540x147.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.39.20-270x73.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"d0c4\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Way easier than defining a new class!<\/p>\n<p id=\"d2ba\" class=\"nn mf gx bf mg ol om hy mj on oo ib mm ic op ie mq if oq ih mu ii or ik my os hu\"><strong>Classes \u201cfor the future\u201d<\/strong><\/p>\n<p class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">Some programmers get obsessed with classes because they\u2019re so flexible and expandable. That\u2019s why, even at reputable companies and seasoned developers, you might encounter code like this:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362468 aligncenter js-lazy\" alt=\"\" width=\"680\" height=\"93\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-280x38.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-540x74.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-270x37.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362468 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44.png\" alt=\"\" width=\"680\" height=\"93\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-280x38.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-540x74.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.44.44-270x37.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<p id=\"37cf\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">The idea behind it is that, as the code grows, this class might be needed for whichever new data structure and the methods that go with it. But this is not a good habit!<\/p>\n<p id=\"4bf5\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Guess what these three lines of code do? Exactly nothing. And those lines are not exactly difficult to code. If you think you\u2019ll need another class later on, and you really think that you could forget about that in the future, you could always leave a comment like this:<\/p>\n<p data-selectable-paragraph=\"\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362469 aligncenter js-lazy\" alt=\"\" width=\"678\" height=\"58\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41.png\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-540x46.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-270x23.png 270w\"\/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1362469 aligncenter\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41.png\" alt=\"\" width=\"678\" height=\"58\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41.png 678w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-280x24.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-540x46.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/Screenshot-2021-07-30-at-11.49.41-270x23.png 270w\"\/><\/noscript><\/figure>\n<\/p>\n<h2 id=\"e138\" class=\"me mf gx bf mg mh mi ku mj mk ml kx mm mn mo mp mq mr ms mt mu mv mw mx my mz hu\"><span class=\"ez-toc-section\" id=\"The_bottom_line_Python_classes_are_a_two-edged_sword\"><\/span>The bottom line: Python classes are a two-edged sword<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"77b6\" class=\"kq kr gx ks b hw na ku kv hz nb kx ky kz nc lb lc ld nd lf lg lh ne lj lk ll gq hu\" data-selectable-paragraph=\"\">Classes are without doubt a powerful concept. Used correctly, they can make your code tidier, more readable, and maintainable.<\/p>\n<p id=\"6ba4\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">But they get<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" class=\"cw kp\" href=\"https:\/\/www.youtube.com\/watch?v=o9pEzgHorH0\">overused a lot<\/a>. And when used wrongly, they can pollute your code until you understand nothing.<\/p>\n<p id=\"2568\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">Sometimes, especially in simpler programs, you could use a class or a bunch of generic functions, and the code would be very similar in its length and complexity. As programs get more complex, the differences get more prominent.<\/p>\n<p id=\"32ae\" class=\"kq kr gx ks b hw kt ku kv hz kw kx ky kz la lb lc ld le lf lg lh li lj lk ll gq hu\" data-selectable-paragraph=\"\">In this sense, the Zen of Python has upheld its verdict: most of the time, there is indeed only one good way of doing things, whether that is with classes or without. It is, however, not always completely obvious. The difficult part is recognizing which way is the good one.<\/p>\n<p data-selectable-paragraph=\"\"><em>This article was written by<span>\u00a0<\/span><span>Rhea Moutafis\u00a0<\/span>and was originally published on<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>. You can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/towardsdatascience.com\/how-to-use-python-classes-effectively-10b42db8d7bd\">read it<\/a><span>\u00a0here.<\/span><\/em><\/p>\n<\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/news\/how-to-use-python-classes-syndication\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to get the most out of Python classes&#8221; There should only be one \u2014 and preferably only one \u2014 obvious way to do it\u201d, says the Zen of Python. Yet there are areas where even seasoned programmers debate what the right or wrong way to do things is. One of these areas is Python&#8230;<\/p>\n","protected":false},"author":1,"featured_media":312961,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/python-class.jpg&signature=1f20b12f536ef06b87b27f4648754de3","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-312960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/312960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=312960"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/312960\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/312961"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=312960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=312960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=312960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}