<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Honey Thakuria</title>
    <description>The latest articles on DEV Community by Honey Thakuria (@honeythakuria).</description>
    <link>https://dev.to/honeythakuria</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F72255%2F3ccbb753-2abb-4106-b3fe-9032d79f421c.jpg</url>
      <title>DEV Community: Honey Thakuria</title>
      <link>https://dev.to/honeythakuria</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/honeythakuria"/>
    <language>en</language>
    <item>
      <title>Object-Oriented Programming in Python</title>
      <dc:creator>Honey Thakuria</dc:creator>
      <pubDate>Sat, 20 Jun 2020 06:23:07 +0000</pubDate>
      <link>https://dev.to/honeythakuria/object-oriented-programming-in-python-33gp</link>
      <guid>https://dev.to/honeythakuria/object-oriented-programming-in-python-33gp</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pfpsXJnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/shbklban3l4fj1skfd4x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pfpsXJnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/shbklban3l4fj1skfd4x.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is always skepticism among the developers, whether Python is an object-oriented language or it's just a more of Procedural language.&lt;/p&gt;

&lt;p&gt;This post will be an answer to such skepticism. Before getting a deep dive into it, let's focus on the fundamental features which Python offers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features of Python:
&lt;/h2&gt;

&lt;p&gt;1.&lt;strong&gt;Simple&lt;/strong&gt; : English like human readable code&lt;/p&gt;

&lt;p&gt;2.&lt;strong&gt;Free &amp;amp; Open Source&lt;/strong&gt;: FLOSS License.&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;Object Oriented&lt;/strong&gt;: Organizing the program by combining data and functionality and wrapping inside object of type defined by Class.&lt;/p&gt;

&lt;p&gt;4.&lt;strong&gt;High Level&lt;/strong&gt;: We don't need to worry about memory management.&lt;/p&gt;

&lt;p&gt;5.&lt;strong&gt;Interpreted&lt;/strong&gt;: We just need to run the source code, it internally converts it into byte code and then translates it to the native language of the computer and then runs it.&lt;/p&gt;

&lt;p&gt;6.&lt;strong&gt;Dynamic &amp;amp; Weak Typed&lt;/strong&gt; : Compiled at run time &amp;amp; Variables can be implicitly coerced to unrelated types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why to take &lt;strong&gt;object-oriented&lt;/strong&gt; approach?
&lt;/h3&gt;

&lt;p&gt;When we have larger problems or when we want to focus on organizing, reusing, minimizing the code, it can be helpful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key terminologies used in object-oriented programming?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Classes&lt;/strong&gt;: It creates a new type. &lt;br&gt;
&lt;strong&gt;Object&lt;/strong&gt;: These are instances of the class. They can store data in variables called &lt;strong&gt;fields&lt;/strong&gt; and also can have functionalities belonging to the class called &lt;strong&gt;methods&lt;/strong&gt; and can store them at the instance or class level. &lt;/p&gt;

&lt;p&gt;A more deep dive into the terminologies will be covered in the next section where we will write some relevant code. &lt;/p&gt;

&lt;h3&gt;
  
  
  Problem Statement:
&lt;/h3&gt;

&lt;p&gt;Keeping track of Students in a School with some common reusable properties like name, age, and some specific properties such as school population, student fees. &lt;br&gt;
It should be designed in such a way that in the future if a new entity like a Teacher or office staff comes, it can reuse the code. &lt;/p&gt;

&lt;h3&gt;
  
  
  Base class: SchoolMember
&lt;/h3&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SchoolMember:
__populationSchool = 0

def __init__(self,name,age,type):
    self.name = name
    self.age = age
    self.type = type
    SchoolMember.__populationSchool +=1

def tell(self):
    print("My name is {0}, My age is {1} and I'm a {2}".format(self.name,self.age,self.type))

@classmethod
def get_schoolPop(cls):
    print(f"School Population is {cls.__populationSchool}")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;
  
  
  Terminologies used:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class&lt;/strong&gt; :A new class can be created using Class statement and name of class. Eg: &lt;em&gt;class SchoolMember:&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods&lt;/strong&gt;: Similar to ordinary functions defined using &lt;em&gt;def&lt;/em&gt; with one addition first parameter, which refers to Object itself. &lt;em&gt;self&lt;/em&gt; is just convention, can rename it too. Eg: &lt;em&gt;def tell(self):&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;strong&gt;init&lt;/strong&gt;&lt;/strong&gt; : A method that is called as soon as the class object gets created. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Class Variable&lt;/strong&gt;:  Variables which can be used by all the instances of the class and share memory. Eg: &lt;em&gt;__populationSchool&lt;/em&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Object Variables&lt;/strong&gt;: Owned by each individual object/instance of the class. Eg: &lt;em&gt;name&lt;/em&gt;,&lt;em&gt;age&lt;/em&gt;  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Private Members&lt;/strong&gt;: Members having double underscore prefixed to them. It can be used within the class only. Eg: &lt;em&gt;__populationSchool&lt;/em&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;@classmethod&lt;/strong&gt;: Decorator to define the class method which shares the memory across all the instance. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Child class inheriting Base class:
&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student(SchoolMember):
__studentPopulation = 0
def __init__(self,name,age,marks,fee):
    SchoolMember.__init__(self,name,age,"Student")
    self.__marks = 80
    self.fee = fee
    Student.__studentPopulation += 1


def studentInfo(self):
    print(f"Student name is {self.name} and his marks is {self.__marks}")

@classmethod
def get_studentPop(cls):
    print(f"Student Population is {cls.__studentPopulation}")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Added terminologies:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inheritance&lt;/strong&gt;: Reusing the code across classes by implementing relationships across classes. Eg:&lt;em&gt;class Student(SchoolMember):&lt;/em&gt; As the name, age are reusable properties, they are inherited from School member and student-specific properties like fee, marks are added to Student class.  Inherited properties can be used in new classes like Teacher, Office Admin, etc, too. &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Creation of Object and usage.
&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(__name__ == "__main__"):
   s1 = Student("Honey",27,80,90000)
   s2 = Student("Shubhi",27,80,920000) 
   s3 = Student("Stu3",27,80,90000)
   s3.get_studentPop() //#Student Population is 3
   s1.tell() //#My name is Honey, My age is 27 and I'm a Student
   s1.studentInfo() //#Student name is Honey and his marks is 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Complete code can be found here: &lt;a href="https://github.com/honey93/CodingProblems/blob/master/Python/oopSchool.py"&gt;https://github.com/honey93/CodingProblems/blob/master/Python/oopSchool.py&lt;/a&gt;&lt;br&gt;
Fork the code, try to put your imagination and come up with Teacher, Office admin, etc classes and master the skills.&lt;/p&gt;

&lt;p&gt;I hope you liked and learned something out of it. &lt;/p&gt;

&lt;p&gt;About me: &lt;a href="https://honeythakuria.com"&gt;https://honeythakuria.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>oop</category>
      <category>python</category>
    </item>
  </channel>
</rss>
