Object Oriented Programming in Python |Episode #13|
Hello and welcome to the python tutorial for beginners, where we teach you python programming from scratch. In the last class, we were learning about File handling or File I/O. We see how to read, write, and append in file and we also saw the use of With Statement. If you have missed that class, don’t worry, CLICK HERE and you are good to go.
So, if you have been this far, then Today’s topic is very Important in Python Programming. Actually, It’s a way of how programs are written. In the first Class, I told you about two types of Programming Paradigm. One was Procedural or Modular Programming and the Other one was Object-Oriented Programming. Procedural Programming we have seen till now, now let’s head to Object-Oriented Programming or OOPs.
Object Oriented Programming
Solving a problem by creating objects is one of the most popular approaches in programming. This is called Object oriented Programming.
This concept focuses on using reusable code or DRY principle which stands for “Do not Repeat Yourself”. This same thing can also be done using Functions and loops as we have seen before. But Object Oriented Programming gives us the extra edge and allows us to make real-world models using our program.
Class – A class is a blueprint for creating objects in OOPs. For ex:
There is a blank form that contains some details like name, phone no., class, etc., and have to be filled by a student. So with that analogy, a blank form is our class, which is kinda template of what we are going to do.
The syntax of class looks like this:
Object – An object is an instantiation of a class. When class is defined, a template is designed. Memory is allocated only after object instantiation. It’s like the blank form, it couldn’t be considered as an application until a student fills and submit it.
Objects of a given Class can invoke the methods available to it without revealing the implementation or details to the user. It is called Abstraction and Encapsulation.
It is like when you want to click a photo and you take out your phone, tap on the camera icon, and click the photo. But is that it? No, there are a lot of processes that run in the background, the moment you click the camera icon. After that many things, post-processing of photo took place and then you get your picture. With OOPs, you can also do this, by doing things in the background, without much user interaction.
Modeling a problem in OOPs
We can identify the following in our problems.
Noun – Class – Employee
Adjective – Attributes – name, age, salary
Verbs – Methods – getsalary(), Increment()
Class Attributes – An attribute that belongs to the class rather than a particular object. Let’s understand this with an example:
Here we make a Class Employee and made a variable company “Google”. Then we instantiate an object by com = Employee(). Now object com can use Employee class and its attributes. So now we print com.company so we got Google so that we can access the class Employee attributes.
We can also update the class attributes, let me show you how.
Here we use Employee.company and change its value to Pied Piper, if you know the reference. Anyway, now the value of the class attribute company has been changed. You can see this in the result, we have print both values before updating and after updating.
Instance Attributes – An attribute that belongs to the instance(object) assuming the class from the previous example.
Here we make a instance attributes instead of using class attribute, com.name, and com.company for the object com.
One important thing to note down here is that – Instance attributes take preference over class attributes during assignment and retrieval. Ex:
Com.attribute1 – 1. is an instance attribute
2. is Class attribute
Self parameter – Self refers to the instance of a Class. It is automatically passed with a function call from an object. Ex:
Here we make a class Employee and make a class method called getsalary and pass a parameter self in it then we calculate the total salary by adding 1500 to the salary. Then we instantiate the object e and instantiate the salary by making an object attribute e.salary, then we call the method by e.getsalary.
But why do we use the self?
Because if we do not pass the self argument then we will get an error that you are passing a value without declaring any argument. That means, even if you are not passing anything but eventually, you are. Sounds confusing? Let me explain when you do e.salary(), you are not passing anything to the function but you are passing salary otherwise there is no point of the method. So, it takes the self as an argument and we use it to do operation on the attributes like self.salary, you can try without self parameter, see what you will get.
Static method – Sometimes we need a function that doesn’t use the self parameter. We can define a static method like this.
Here we make a Class User then make a method greet but we didn’t give it use self here. Why? Because we have use @staticmethod decorator here, which makes the method static.
init() Constructor – It is a special method that is first run as soon as the object is created. init() method is also known as Constructor.
It takes self argument and can also take further arguments. For ex:
Here we make a Class employee then make the constructor by using init and passing three arguments: self, name and company, and then further making attributes self.name, self.company. Then we make another method called getinfo() which prints the employee info. Then we instantiate the object e but this time we pass the arguments “Kevin” and “Google” at instantiation as we have use constructor which run at the time object is created. Then we call the get info function.
So, that’s it for today. Today we cover a hell of a lot of topics, let’s revise what we learned today. First, we saw Object Oriented Programming basics like class and objects. Then we see class and object attributes. Then we saw the self parameter followed by the static method and at last, the init constructor. The OOPs concepts are really very helpful in making real-world applications. So, practice is a must. On that note, this is Dolores Haze, signing off, I will meet you in the next one, if you want to see more on Object-Oriented Programming, CLICK HERE. Till then “Happy Coding” and Bye.