{"id":6894,"date":"2021-07-31T13:00:33","date_gmt":"2021-07-31T13:00:33","guid":{"rendered":"http:\/\/TheNextWeb=1362454"},"modified":"2021-07-31T13: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:\/\/www.londonchiropracter.com\/?p=6894","title":{"rendered":"How to get the most out of Python classes"},"content":{"rendered":"\n<p>There should only be one \u2014 and preferably only one \u2014 obvious way to do it\u201d, says the <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0020\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">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>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><strong>Python classes: the very basics<\/strong><\/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<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362455 aligncenter js-lazy\" 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\" sizes=\"(max-width: 681px) 100vw, 681px\" data-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 decoding=\"async\" loading=\"lazy\" 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>\n<p><span>Now, we can define various instances of the class and keep them organized:<\/span><\/p>\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362456 aligncenter js-lazy\" 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\" sizes=\"(max-width: 680px) 100vw, 680px\" data-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 decoding=\"async\" loading=\"lazy\" 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>\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<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362457 aligncenter js-lazy\" 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\" sizes=\"(max-width: 676px) 100vw, 676px\" data-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 decoding=\"async\" loading=\"lazy\" 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>\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<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362458 aligncenter js-lazy\" 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\" sizes=\"(max-width: 677px) 100vw, 677px\" data-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 decoding=\"async\" loading=\"lazy\" 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>\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<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362459 aligncenter js-lazy\" 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\" sizes=\"(max-width: 679px) 100vw, 679px\" data-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 decoding=\"async\" loading=\"lazy\" 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 decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362460 aligncenter js-lazy\" 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\" sizes=\"(max-width: 678px) 100vw, 678px\" data-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 decoding=\"async\" loading=\"lazy\" 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 decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362461 aligncenter js-lazy\" 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\" sizes=\"(max-width: 677px) 100vw, 677px\" data-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 decoding=\"async\" loading=\"lazy\" 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 decoding=\"async\" loading=\"lazy\" class=\"wp-image-1362462 js-lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/07\/unsplash.jpeg\" alt width=\"514\" height=\"684\" sizes=\"(max-width: 514px) 100vw, 514px\" data-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 href=\"https:\/\/thenextweb.com\/news\/how-to-use-python-classes-syndication#\" 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\"><\/i><\/a>Classes are powerful concepts when used right. Photo by Muhannad Ajjan on Unsplash<\/figcaption><noscript><img decoding=\"async\" loading=\"lazy\" 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\">Classes are amazing \u2014 in theory<\/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 science, 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>Inheritance: writing the DNA of a data structure<\/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\">When to use classes<\/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>&nbsp;<\/span><a class=\"cw kp\" href=\"https:\/\/www.reddit.com\/r\/learnpython\/comments\/6wsjt9\/i_still_dont_get_when_to_use_class_vs_functions\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">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\">When classes are a bad idea<\/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 href=\"https:\/\/devblogs.microsoft.com\/python\/idiomatic-python-functions-versus-classes\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">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 decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362464 aligncenter js-lazy\" 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\" sizes=\"(max-width: 678px) 100vw, 678px\" data-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 decoding=\"async\" loading=\"lazy\" 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 decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362468 aligncenter js-lazy\" 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\" sizes=\"(max-width: 680px) 100vw, 680px\" data-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 decoding=\"async\" loading=\"lazy\" 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 decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1362469 aligncenter js-lazy\" 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\" sizes=\"(max-width: 678px) 100vw, 678px\" data-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 decoding=\"async\" loading=\"lazy\" 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\">The bottom line: Python classes are a two-edged sword<\/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>&nbsp;<\/span><a class=\"cw kp\" href=\"https:\/\/www.youtube.com\/watch?v=o9pEzgHorH0\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">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>&nbsp;<\/span><span>Rhea Moutafis&nbsp;<\/span>and was originally published on<span>&nbsp;<\/span><a href=\"https:\/\/towardsdatascience.com\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Towards Data Science<\/a>. You can <a href=\"https:\/\/towardsdatascience.com\/how-to-use-python-classes-effectively-10b42db8d7bd\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">read it<\/a><span>&nbsp;here.<\/span><\/em><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/news\/how-to-use-python-classes-syndication\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6895,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/6894"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6894"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/6894\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/6895"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}