{"id":1893,"date":"2019-06-10T13:54:47","date_gmt":"2019-06-10T13:54:47","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1893"},"modified":"2019-06-11T13:07:17","modified_gmt":"2019-06-11T13:07:17","slug":"section-8-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1893","title":{"rendered":"Section 8: Object Oriented Programming"},"content":{"rendered":"<h1>Section 8 Menu<\/h1>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1885\">&lt; Section 7<\/a> | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1901\">Section 9 &gt;<\/a><\/p>\n<ol start=\"59\">\n<li>Introduction<\/li>\n<\/ol>\n<h1>Introduction<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478286#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478286#questions<\/a><\/p>\n<p>Object Oriented Programming (OOP) allows programmers to create their own objects that have4 methods and attributes. Recall that after defining a string, list, dictionary or other objects, you are able to call methods off them with the .method_name() syntax.<\/p>\n<p>These methods act as functions that use information about the object, as well as\u00a0 the object itself to return results, or change the current object.<\/p>\n<ul>\n<li>Examples:\n<ul>\n<li>Appending an item to a list<\/li>\n<li>counting the occurrences of an element in a tuple.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>OOP allows users to create their own objects. In general, OOPs allows us to create code that is repeatable and organized.<\/p>\n<p>For much larger scripts of Python code, functions by themselves aren&#8217;t enough for organization and repeatability.\u00a0 Commonly repeated tasks and objects can be defined with OOP to create code that is more usable.<\/p>\n<h2>Class<\/h2>\n<p>Objects are defined using the &#8216;class&#8217; keyword. Objects are often called &#8216;classses&#8217;<\/p>\n<ul>\n<li>Use CamelCase where every word is capitalized.<\/li>\n<\/ul>\n<pre>class NameOfClass():<\/pre>\n<h2>__init__ method<\/h2>\n<p>Allows you to create an instance of the actual object.<\/p>\n<ul>\n<li>self is a keyword that allows class methods to reference the class&#8217;s attributes<\/li>\n<li>assign the parameters passed in as attributes to the object.\u00a0 This prevents mistaking them from global variables.<\/li>\n<\/ul>\n<pre>def __init__(self, param1, param2, ...):\r\n    self.param1 = param1\r\n    self.param2 = param2<\/pre>\n<h2>Object methods<\/h2>\n<p>Functions that support the object. Use these methods to interact with the object.<\/p>\n<pre>def some_method(self):\r\n    #do some stuff<\/pre>\n<h1>Attributes and Class Keyword<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478292#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478292#questions<\/a><\/p>\n<h2>Simplest Class<\/h2>\n<pre>class Sample():\r\n    pass<\/pre>\n<h2>__init__ constructor<\/h2>\n<ul>\n<li>Is called automatically when a new object is created.<\/li>\n<li>&#8216;self&#8217; refers it itself.\u00a0 This is usually called &#8216;behind the scenes&#8217; with other languages, but is displayed in Python.\n<ul>\n<li>self &#8216;could&#8217; be another variable, but strongly recommended to use &#8216;self&#8217;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>class Dog():\r\n    def __init___(self, breed):\r\n        self.breed = breed<\/pre>\n<pre>my_dog = Dog()<\/pre>\n<p>TypeError: __init__() missing 1 required positional argument: &#8216;breed&#8217;<\/p>\n<pre>my_dog = Dog(breed='Lab')\r\ntype(my_dog)<\/pre>\n<p>__main__.Dog<\/p>\n<pre>my_dog.breed<\/pre>\n<p>&#8216;Lab&#8217;<\/p>\n<h1>Class Object Attributes and Methods<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478294#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478294#questions<\/a><\/p>\n<p>These allow you to define attributes that are standard or shared among all instances of the object vs. attributes that are specific to each object.<\/p>\n<h3>Class Object Example<\/h3>\n<pre>class Dog():\r\n    # Class Object Attributes\r\n    # These are the same for any instance of the class.\r\n    # note that the 'self' keyword is NOT used here.\r\n    species = 'mammal'\r\n\r\n    def __init___(self, breed, name):\r\n        self.breed = breed<\/pre>\n<p>&nbsp;<\/p>\n<h2>Methods<\/h2>\n<p>Functions associated with an object.<\/p>\n<pre>    def bark(self, number):\r\n        for i in range(0,number):\r\n            print(\"Woof!  My name is {}\".format(self.name))\r\n        print(\"I am a {}.\".format(self.species))\r\n        print(\"I am a {}.\".format(Dog.species))<\/pre>\n<pre>my_dog = Dog('Lab', 'Shotgun')\r\nmy_dog.bark(2)<\/pre>\n<p>Woof! My name is Shotgun<br \/>\nWoof! My name is Shotgun<br \/>\nI am a mammal.<br \/>\nI am a mammal.<\/p>\n<h1>Inheritance and Polymorphism<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478298#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478298#questions<\/a><\/p>\n<p>&lt;h2&gt;Inheritance&lt;\/h2&gt;<\/p>\n<p>Using classes that have already been defined.<\/p>\n<pre>class Animal():\r\n    def __init__(self):\r\n        print(\"Animal created\")\r\n    def who_am_i(self):\r\n        print(\"I am an animal.\")\r\n    def eat(self):\r\n        print(\"I am eating.\")<\/pre>\n<pre>myanimal = Animal()\r\nmyanimal.who_am_i()\r\nmyanimal.eat()<\/pre>\n<p>Animal created<br \/>\nI am an animal.<br \/>\nI am eating.<\/p>\n<h3>Create a new dpg class that inherits Animal and can use it&#8217;s methods<\/h3>\n<pre>class Dog(Animal):\r\n    def __init(self):\r\n        Animal.__init__(self)<\/pre>\n<pre>mypet = Dog()\r\nmypet.eat()<\/pre>\n<h2>Polymorphism<\/h2>\n<p>Polymorphism refers to the way different object classes can share the same method name.<\/p>\n<pre>class Dog():\r\n    def __init__(self, name):\r\n        self.name = name\r\n    def speak(self):\r\n        return self.name + \" says 'Woof!'\"\r\nclass Cat():\r\n    def __init__(self, name):\r\n        self.name = name\r\n    def speak(self):\r\n        return self.name + \" says 'Meow!'\"<\/pre>\n<pre>puppy = Dog('Spot')\r\nkitty = Cat('Fluffy')\r\nprint(puppy.speak())\r\nprint(kitty.speak())<\/pre>\n<p>Spot says &#8216;Woof!&#8217;<br \/>\nFluffy says &#8216;Meow!&#8217;<\/p>\n<pre>for pet in [puppy, kitty]:\r\n    print(type(pet))\r\n    print(pet.speak())<\/pre>\n<p>&lt;class &#8216;__main__.Dog&#8217;&gt;<br \/>\nSpot says &#8216;Woof!&#8217;<br \/>\n&lt;class &#8216;__main__.Cat&#8217;&gt;<br \/>\nFluffy says &#8216;Meow!&#8217;<\/p>\n<pre>def pet_speak(pet)\r\n    print(pet.speak())<\/pre>\n<pre>pet_speak(kitty)<\/pre>\n<p>Fluffy says &#8216;Meow!&#8217;<\/p>\n<h2>Abstract Classes<\/h2>\n<p>Abstract classes are classes that are never expected to instantiated themselves, but are created strictly to serve as a base class for other classes.<\/p>\n<pre>class Animal():\r\n    def __init__(self, name):\r\n        self.name = name\r\n    def speak(self):\r\n        raise NotImplementedError(\"Subclass must implement this abstract method\")<\/pre>\n<pre>myanimal = Animal('Fluffy')\r\nmyanimal.speak()<\/pre>\n<p>NotImplementedError: Subclass must implement this abstract method<\/p>\n<pre>class Dog(Animal):\r\n    def speak(self):\r\n        return self.name + \" Says 'Wooooof!!'\"<\/pre>\n<pre>mydog = Dog('Fido')\r\nprint(mydog.speak())<\/pre>\n<p>Fido Says &#8216;Wooooof!!&#8217;<\/p>\n<p>41% course complete<\/p>\n<h1>Special Magic \/ Dundar Methods<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478306#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478306#questions<\/a><\/p>\n<h2>__str__, __len__, __del__<\/h2>\n<pre>class Book():\r\n    def __init__(self,title, author, pages):\r\n        self.title=title\r\n        self.author=author\r\n        self.pages=pages\r\n    def __str__(self):\r\n        return f\"{self.title} by {self.author}\"\r\n    def __len__(self):\r\n        return self.pages\r\n    def __del__(self):\r\n        print(f\"Book {self.title} by {self.author} has been deleted.\")\r\nb = Book('my book','me',3)\r\nprint(b)\r\nprint(len(b))\r\ndel b\r\nprint(b)<\/pre>\n<p>mybook by me<br \/>\n3<br \/>\nBook my book by me has been deleted.<br \/>\nNameError: name &#8216;b&#8217; is not defined<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Section 8 Menu &lt; Section 7 | Section 9 &gt; Introduction Introduction https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9478286#questions Object Oriented Programming (OOP) allows programmers to create their own objects that have4 methods and attributes. Recall that after defining a string, list, dictionary or other objects, you are able to call methods off them with the .method_name() syntax. These methods act ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1893\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1893","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1893"}],"version-history":[{"count":7,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":1903,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions\/1903"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}