{"id":207,"date":"2015-08-02T22:07:28","date_gmt":"2015-08-02T22:07:28","guid":{"rendered":"http:\/\/tuxlabs.com\/?p=207"},"modified":"2015-11-03T18:10:41","modified_gmt":"2015-11-03T18:10:41","slug":"object-oriented-programming-with-python-pillar-one-encapsulation","status":"publish","type":"post","link":"https:\/\/tuxlabs.com\/?p=207","title":{"rendered":"Object-Oriented Programming With Python : Encapsulation (1\/3)"},"content":{"rendered":"<h2>Justification For Learning OOP<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-215 alignleft\" src=\"http:\/\/tuxlabs.com\/wp-content\/uploads\/2015\/08\/linux-python-logo1.jpg\" alt=\"linux-python-logo1\" width=\"80\" height=\"80\" \/>Since 1995,\u00a0I have written some form of code, but over the years my career has never taken shape with a primary focus on development. Instead in my various roles of Systems, Network, Application and Datacenter engineering &amp; architecture, I have found excuses to let the inner code hacker in me, out. Allowing me to consistently pursue my passion for coding and automation within the scope of my daily duties. However, this approach has never allowed me to work on a development team and thus the necessity for using object oriented programming rarely has occurred.<\/p>\n<p>Sure I&#8217;ve written classes and modules for re-use, but I have never had to place a tremendous amount of forethought into the design for flexibility or inflexibility of the interfaces those classes provide because I was the only one using them. This hasn&#8217;t change, but as of late, I find myself realizing my career may be ready for a change. It is likely the next step for me in my career is a full blown software development role. This is partly because of boredom and mastery in my existing field after decades of experience, but mostly because of my own career\u00a0planning and the apparent industry trends.<\/p>\n<p>Simply put more jobs are moving to software development, and Systems Engineering is on a steady decline due to the advent of Cloud computing, Containers and more. Due to the amazing products, platforms and tools that are now available less and less Systems Engineers are required and I would say Systems Engineers that don&#8217;t code are already obsolete, whether they realize it yet or not.<\/p>\n<p>In todays world if you are or are going to be a developer, knowing how to write object oriented code is a requirement and more importantly thinking ahead about who and how your code will be used becomes paramount when designing\u00a0effective software. This article is intended to help you get a better understanding of how to write object oriented programming using Python.<\/p>\n<h2>Procedural Programming vs. Object Oriented Programming<\/h2>\n<p>Procedural\u00a0programming\u00a0 is a more antiquated\u00a0paradigm\u00a0for software design that can be described as:<\/p>\n<ul>\n<li>Code that processes data<\/li>\n<li>The data we process is stored in variables<\/li>\n<li>Then we create functions to\u00a0process the data in those variables<\/li>\n<\/ul>\n<p>In the object oriented programming paradigm:<\/p>\n<ul>\n<li>We organize data into Objects<\/li>\n<li>Functions become\u00a0Methods<\/li>\n<li>And The design of these objects &amp; methods is specified in a Class, which is essentially a blue print.<\/li>\n<\/ul>\n<p>Object oriented programming is the primary software design paradigm in existence today because it provides programmers with a way to effectively organize and share code for re-use while to protecting the integrity of\u00a0the existing code. To understand how this works we need to jump right into it with <strong>The Three Pillars of Object Oriented Programming.<\/strong><\/p>\n<h2>The Three Pillars (Encapsulation, Inheritance, and Polymorphism)<\/h2>\n<h3>Encapsulation<\/h3>\n<p>Encapsulation is about ensuring the\u00a0safe storage of data as attributes in an instance.<\/p>\n<p>Encapsulation tells us that :<\/p>\n<ul>\n<li>Data should only be accessed through\u00a0instance methods.<\/li>\n<li>Data should always be correct based on the validation requirement set in the class methods.<\/li>\n<li>And Data should be safe from changes by external processes.<\/li>\n<\/ul>\n<p>Great so what does that mean \ud83d\ude42 It means we should be using &#8216;setter&#8217; and &#8216;getter&#8217; methods to access object attribute values. Here&#8217;s an example of a class that uses a setter and getter method (note the name setter &amp; getter is not actually required) to set and get the variable\/attribute value.<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\r\n\r\nclass Tuxlabs(object):\r\n    def setter(self, website):\r\n        self.website = website\r\n\r\n    def getter(self):\r\n        return self.website\r\n\r\n\r\ntuxlabs = Tuxlabs()\r\n\r\ntuxlabs.setter('http:\/\/www.tuxlabs.com')\r\nprint (tuxlabs.getter())\r\n\r\ntuxlabs.setter('http:\/\/www.google.com')\r\nprint (tuxlabs.getter())<\/pre>\n<p><strong>The output of this code is:\u00a0<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\u279c  tuxlabs  python example.py \r\nhttp:\/\/www.tuxlabs.com\r\nhttp:\/\/www.google.com\r\n\u279c  tuxlabs<\/pre>\n<p>It&#8217;s important to recognize that encapsulation is not enforced by Python. So a programmer using a\u00a0class is not required to access the data through the getter and setter methods. For example, in the class above someone could set website, without ever interacting with the defined setter and getter classes. This is called <strong>breaking encapsulation,<\/strong> and it&#8217;s bad form \/ practice for a programmer to this because the class\u00a0author &amp; maintainer can no longer validate the data that is being accessed and this can cause unforeseen problems with a program that is dependent on the class it is inheriting from. <strong>Here&#8217;s a modified example where we are breaking encapsulation.\u00a0<\/strong><\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\r\n\r\nclass Tuxlabs(object):\r\n    def setter(self, website):\r\n        if 'http' in website:\r\n            self.website = website\r\n        else:\r\n            print 'ERROR: Unable to set website to %s.\\nRequired format is: http:\/\/www.example.com' % (website)\r\n\r\n    def getter(self):\r\n        output = 'Website is set to: ' + str(self.website) + '\\n'\r\n        return output\r\n\r\ntuxlabs = Tuxlabs()\r\n\r\nwebsite = 'http:\/\/www.tuxlabs.com'\r\nprint \"Attempting to set website to: \\\"%s\\\"\" % (website)\r\ntuxlabs.setter(website)\r\nprint (tuxlabs.getter())\r\n\r\nwebsite = 'http:\/\/www.google.com'\r\nprint \"Attempting to set website to: \\\"%s\\\"\" % (website)\r\ntuxlabs.setter(website)\r\nprint (tuxlabs.getter())\r\n\r\nwebsite = 'I should not be accessing website in the class directly!, but since I am breaking encapsulation it does still work, it\\'s just naughty!'\r\nprint \"Attempting to set website to: \\\"%s\\\"\" % (website)\r\ntuxlabs.website = website\r\nprint tuxlabs.website ## Printing without the getter doh !\r\n\r\nprint \"\"\r\nwebsite = 'This is not a website, so an error is thrown!'\r\nprint \"Attempting to set website to: \\\"%s\\\"\" % (website)\r\ntuxlabs.setter(website)\r\n<\/pre>\n<p><strong>Here is the output of the above example:\u00a0<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\u279c tuxlabs python example.py\r\nAttempting to set website to: \"http:\/\/www.tuxlabs.com\"\r\nWebsite is set to: http:\/\/www.tuxlabs.com\r\n\r\nAttempting to set website to: \"http:\/\/www.google.com\"\r\nWebsite is set to: http:\/\/www.google.com\r\n\r\nAttempting to set website to: \"I should not be accessing website in the class directly!, but since I am breaking encapsulation it does still work, it's just naughty!\"\r\nI should not be accessing website in the class directly!, but since I am breaking encapsulation it does still work, it's just naughty!\r\n\r\nAttempting to set website to: \"This is not a website, so an error is thrown!\"\r\nERROR: Unable to set website to This is not a website, so an error is thrown!.\r\nRequired format is: http:\/\/www.example.com\r\n\u279c tuxlabs<\/pre>\n<p>A number of things have changed.<\/p>\n<ol>\n<li>We added a variable website to make the example more clear and easy to follow.<\/li>\n<li>We added validation to our setter() method, it now checks to make sure &#8216;http&#8217; is contained in the value that we are setting for website.<\/li>\n<li>If &#8216;http&#8217; is not in website the setter() method will throw an error and will not set website!<\/li>\n<li>We added some additional formatting to our getter() method so we can tell when it is being used verses when encapsulation is being broken. getter() now pre-pends any website passed with &#8216;Website is now set to:&#8217;<\/li>\n<\/ol>\n<p>So let&#8217;s walk thru how these changes effected the program and what actually happened. First, we tried to set website to &#8216;http:\/\/www.tuxlabs.com&#8217; and then we changed it to &#8216;http:\/\/www.google.com&#8217;. In both of these examples we followed the rules of encapsulation, setting and getting the attribute value through methods in the class. These examples are correct and set website to the value passed and outputed them with the correct formatting we implemented in getter().<\/p>\n<p>Next, we tried setting website to a really long string, without using setter() ! So we did tuxlabs.website = website directly on the instance of our class ! We also circumvented using getter() and just printed in exactly the same, broken way print tuxlabs.website. Unfortunately, as previously mentioned nothing requires the programmer to use our setter and getter methods directly, and thus website is not being checked for containing &#8216;http&#8217; here and so it can printed our very long string (&#8220;I should not be accessing website in the class directly!, but since I am breaking encapsulation it does still work, it\\&#8217;s just naughty!&#8221;).<\/p>\n<p>The last and final example, is an example of encapsulations value proposition. We try to set website to something that does not contain &#8216;http&#8217; and therefore is not a website. An error is thrown and website is not set, which is exactly what we want.<\/p>\n<p>Encapsulation is \u00a0simple, easy to follow and powerful when your programming on or with a team of developers that are going to share code amongst each other. In these scenarios encapsulation is obviously a must promoting collaboration through protecting programmers from potentially mis-using each others code. It should be easy to see now that breaking encapsulation is bad and very poor practice because code that perfectly functions one day, could be broken the next without any changes from you, because you are violating the contract that encapsulation methods of the class\u00a0provide.<\/p>\n<p>This concludes our learning about encapsulation. Still to come, Inheritance, Polymorphism, and some real world examples!<\/p>\n<p>Until next time&#8230;Keep learning,<br \/>\nJason Riedel<\/p>\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/tuxlabs.com\/?p=207\" rel=\"bookmark\" title=\"Permalink to Object-Oriented Programming With Python : Encapsulation (1\/3)\"><p>Justification For Learning OOP Since 1995,\u00a0I have written some form of code, but over the years my career has never taken shape with a primary focus on development. Instead in my various roles of Systems, Network, Application and Datacenter engineering &amp; architecture, I have found excuses to let the inner code hacker in me, out. [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,9],"tags":[50,48,47,49,55,51,52],"class_list":{"0":"post-207","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-programming","7":"category-python","8":"tag-encapsulation","9":"tag-inheritence","10":"tag-object-oriented-programming","11":"tag-polymorphism","12":"tag-python","13":"tag-the-3-pillars","14":"tag-three-pillars","15":"h-entry","16":"hentry"},"_links":{"self":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207"}],"version-history":[{"count":16,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}