{"id":733,"date":"2022-02-24T14:08:15","date_gmt":"2022-02-24T19:08:15","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=733"},"modified":"2024-02-09T14:43:53","modified_gmt":"2024-02-09T19:43:53","slug":"object-oriented-python","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/object-oriented-python\/","title":{"rendered":"Object-Oriented Python"},"content":{"rendered":"\n<p>Python is a fully object-oriented programming language but it can also be used for scripting. It is assumed that you are already familiar with object-orientation concepts and have experience writing object-oriented code in another language. This page will show how to write object-oriented code in Python without explaining object-oriented concepts.<\/p>\n\n\n\n<p>The following class computes the area of a rectangle. Classes are defined with class keyword.&nbsp;<strong>init<\/strong>&nbsp;is the constructor. length and width are initialized in the constructor. Area is calculated by calcArea().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># class definition\nclass Rectangle():\n\n   # constructor\n    def __init__(self, length, width):\n        self.length = length\n        self.width = width\n\n    # calculate area\n    def calcArea(self):\n        return self.length * self.width\n\n# run the code\nr = Rectangle(23, 32)\nprint(r.calcArea())       # output = 736<\/code><\/pre>\n\n\n\n<p>To use the class, we first create and object, r and then call r.calcArea() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inheritance<\/h3>\n\n\n\n<p>Following example shows how to use inheritance in Python3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle():\n\n    def __init__(self, make, model):\n        self.make = make\n        self.model = model\n\n    def __str__(self):\n        return self.make + \" \" + self.model\n\nclass Car(Vehicle):\n\n    def __init__(self, make, model, wheels):\n        Vehicle.__init__(self, make, model)\n        self.wheels = wheels\n\n    def __str__(self):\n        return Vehicle.__str__(self) + \" has \" + self.wheels + \" wheels\"\n\nv = Vehicle('Toyota','Corolla')\nprint(v)             \nm = Car('Toyota','Corolla','four')\nprint(m)<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Toyota Corolla\nToyota Corolla has four wheels<\/code><\/pre>\n\n\n\n<p>Vehicle is the base class. Car is the subclass. Note that declaration of the class Car takes superclass name as input parameter. Also note the use of Vehicle.<strong>init<\/strong>&nbsp;in Car class to initialize superclass variables and the use of Vehicle.<strong>str<\/strong>(self) in the&nbsp;<strong>str<\/strong>&nbsp;method to print values from superclass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Polymorphism<\/h3>\n\n\n\n<p>Polymorphism basically refers to the same method call doing different things for different subclasses. In the following example, seats() method returns different results based on which subclass is called. The property name in the super class is used by all subclasses.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle():\n    def __init__(self, name):\n        self.name = name\n\n    # abstract method\n    def seats(self):\n        raise NotImplementedError(\"Subclass must implement abstract method\")\n\nclass Sedan(Vehicle):\n    def seats(self):\n        return '4'\n\nclass Minivan(Vehicle):\n    def seats(self):\n        return '7'\n\nclass Motorbike(Vehicle):\n    def seats(self):\n        return '2'\n\nvehicles = &#91;Sedan('Tesla Model S'),\n            Minivan('Toyota Sedona'),\n            Motorbike('Harley Davidson')]\n\nfor vehicle in vehicles:\n    print(vehicle.name + ' has ' + vehicle.seats() + ' seats')<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tesla Model S has 4 seats\nToyota Sedona has 7 seats\nHarley Davidson has 2 seats<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python is a fully object-oriented programming language but it can also be used for scripting. It is assumed that you are already familiar with object-orientation concepts and have experience writing object-oriented code in another language. This page will show how to write object-oriented code in Python without explaining object-oriented concepts. The following class computes the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[76,137],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-python","tag-programming","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":734,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/733\/revisions\/734"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}