Object-Oriented Programming in Python
Tags: programming, oop, python
Published: 2020-06-20 06:23:07

---
title: Object-Oriented Programming in Python
published: true
description: Learning Object-Oriented Programming using Python.

//cover_image: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/shbklban3l4fj1skfd4x.jpg)

tags: programming, oop, Python

---
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/shbklban3l4fj1skfd4x.jpg)


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

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.

##Features of Python:
1.**Simple** : English like human readable code

2.**Free & Open Source**: FLOSS License.

3.**Object Oriented**: Organizing the program by combining data and functionality and wrapping inside object of type defined by Class.

4.**High Level**: We don't need to worry about memory management.

5.**Interpreted**: 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.

6.**Dynamic & Weak Typed** : Compiled at run time & Variables can be implicitly coerced to unrelated types.


### Why to take **object-oriented** approach?
When we have larger problems or when we want to focus on organizing, reusing, minimizing the code, it can be helpful.

### Key terminologies used in object-oriented programming?
**Classes**: It creates a new type. 
**Object**: These are instances of the class. They can store data in variables called **fields** and also can have functionalities belonging to the class called **methods** and can store them at the instance or class level. 

A more deep dive into the terminologies will be covered in the next section where we will write some relevant code. 

###Problem Statement: 
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. 
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. 


###Base class: SchoolMember

    
    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}")


####Terminologies used: 
- **Class** :A new class can be created using Class statement and name of class. Eg: *class SchoolMember:*
- **Methods**: Similar to ordinary functions defined using *def* with one addition first parameter, which refers to Object itself. *self* is just convention, can rename it too. Eg: *def tell(self):*
- **__init__** : A method that is called as soon as the class object gets created. 
- **Class Variable**:  Variables which can be used by all the instances of the class and share memory. Eg: *__populationSchool* 

- **Object Variables**: Owned by each individual object/instance of the class. Eg: *name*,*age*  
- **Private Members**: Members having double underscore prefixed to them. It can be used within the class only. Eg: *__populationSchool* 

- **@classmethod**: Decorator to define the class method which shares the memory across all the instance. 



###Child class inheriting Base class:

    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}")

### Added terminologies:
- **Inheritance**: Reusing the code across classes by implementing relationships across classes. Eg:*class Student(SchoolMember):* 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. 



### Creation of Object and usage.
    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


Complete code can be found here: https://github.com/honey93/CodingProblems/blob/master/Python/oopSchool.py
Fork the code, try to put your imagination and come up with Teacher, Office admin, etc classes and master the skills.

I hope you liked and learned something out of it. 

About me: https://honeythakuria.com