Hello guys. Welcome to LearnVern. In the last topic we saw Exception Handling, where we learned how to handle the errors, the exceptions in Python.
Today we will move onto advanced Python where we will see how Object Oriented Programming works in Python.
We are going to see the 6 concepts of Object Orientation. We will see both Theory & Practicals in this video.
So our 6 Object Oriented Concepts are -
Class, Objects, Inheritance, Polymorphism, Abstraction & Encapsulation.
These 6 concepts help Object Oriented languages to operate such as Java, Php,
. (dot) Net, Python.
Our first concept is class.
What is class?
Class is 1 type of Boundary in which you can define functions, methods, variables.
You can say in 1 way that when you go to buy a home or make a home, you have to buy the plot.
That plot is 1 type of class where you will either make a home or buy one.
Class is recognised by various names in different topics such as Base Class, Drive Class, Predefined Class, etc.
These are some names through which class is known.
Now how to Define Class?
How to create a class?
Python has a keyword named Class, with which you can create a class & you can name it & further add a body as well.
This is how a class is made in Python.
It will be clearer in practicals.
Moving ahead to see what is Object.
Object is 1 type of instance of class.
Meaning you cannot access the property of the class without an object.
Suppose you created a variable, a function or any constructor as such in class, you have to use Object to access it.
Which further means without Object, you cannot access any property of Class.
What is a Constructor?
Constructor is a special member function which is called when the object of your class is made.
As soon as the object of your class is made, the constructor is called.
Constructor is identified by underscore.
The init function is used to recognise Constructor in Python.
Which function? Init function. It is called the Initialisation function.
This function is called a constructor in Python & it is specially called when the object is created of any class.
How to create a Constructor.
Here, it is created with Def in the same way as function.
Add underscore underscore init...self.
And then the constructor body.
So this was the theoretical part. We will move onto the practical part now.
I will open Jupyter now. First of all I’ll check how to create a class.
I’ll markdown first of all how to create a class.
You simply have to write Class here & write its name myclass.
So our class is created. I wrote the X = 5 variable.
We have defined a variable into a class.
Now I’ll directly print the class.
It is very secure here so it will give an answer in this way.
You cannot directly access the class’s property.
So the property of class is our variable.
I’ll write here, property of class.
To access the X’s value of 5, you will have to create an object.
Let’s see how to create an object.
I’ll write here, how to create an object.
I have to make a myclass object.
So I’ll make myclass again. Where I took X = 5 which is our property of class.
Now what if I have to print it? I’ll tell you.
Creating object of my class.
You have to take an object’s name. It can be anything.
Suppose I took the name m1 & have to define the name of the class that is myclass.
Now how to access the value of X.
M1 . (dot) X.
I’ll print it now. And run it.
Now you can access the property of your class by using object.
M1 is an object of class myclass.
Inshort to access the value of X = 5, you made an object & printed it with the help of the same object.
This is how the object works in class.
You can use any, any particular class’s property through Object only.
Without Object you cannot access Class Property.
Moving ahead to see how to call a function through Object
Calling a function by Object.
So I made a class here named myclass.
Now I have to make a function.
Def myfunction. I’ll simply print Hello myfunction.
This is one type of property of myclass.
I am now creating an object here.
Creating an object.
I created an object called m1, myclass.
Now I have to call this function. I want to call the myfunction one.
I will call myfunction in this manner.
But when I run it, it will show me an error.
What was the error?
That myfunction should have 0 positional arguments.
But why? Here whenever we make a function in class, you have to compulsory write Self within the parameter of that function.
Because Self is a predefined instance of class which shows which particular class you belong to.
As soon as I run it, it will give me an answer.
See, you can see the output here.
So this is how you can call any function through Object.
Moving ahead…
How to create a Constructor of Class.
How to create a Constructor.
I made a class here named myclass.
Now whenever you want to create a Constructor in this.
I’ll write Creating a Constructor.
Write Def. underscore underscore init. Again underscore underscore.
Now this function is under class. It is compulsory to write Self like I said before.
Now I’ll print “this is my constructor”
Now when will the constructor be called?
When the object of this class is created.
Creating an object.
We will create an object like we saw before.
M1 = myclass.
Now we don’t need to call Init function, it will automatically be called.
As soon as you created the object, the constructor got called automatically.
You won’t need to call the init function.
Once the object is made, the constructor is called by default.
Now how to change the value through a specific object.
Change the value by object.
I’ll make a class here named myclass.
In which I made a constructor, Init. in which I wrote Self.
And passed 2 parameters: name & age.
We have to initialise values with Self which the user passes.
Self . (dot) name = name.
Self . (dot) age = age.
The self.age got converted from variable into class.
I’ll make 1 more function named myfunction.
I’ll write Self in it. And print My Name is.
I’ll print it here as Self . (dot) name.
The one I initialised before.
Now I'll make an object. Creating an object.
Suppose I took an m1 named object of myclass.
I’ll simply pass the value in myclass object.
How will we pass it? The init function will be called by default.
I’ll pass Sunit, and his age is supposed to be 25.
As I run it, nothing will change because our constructor hasn’t been called yet.
Now as soon as I write m1.age. I’ll print it. That Is the self one.
You can see the output here.
As we wrote m1.age, the age appeared to be 25.
Then we printed it.
I’ll print m1.name in the same way. And we got the name.
Our current age is 25 but now I want to change it.
Changing the age using object.
M1.age = 26.
Now I’ll run it. And then print Age + m1.age.
It told me that Int is not like a string because we wrote + here.
We use + only & only for string.
If you want to print a value, if you do it like this, it will be updated.
Before the update the age was 25.
And after updating, we entered 26 so now our age is 26.
So this is how you can change the values with object.
Now how to delete some object?
Now I’ll write here. Delete the object.
How to do it? Write Del m1.age.
We want to delete the age.
Now when I print Age, it will give me an error.
A message saying Myclass object has no attribute “age”.
Because we had deleted our Age variable.
So this is how we can work around class & object in Python.
So what did we see today?
We saw how to create a class, how to create an object, how to call a function with object, how to create Constructor, and how to change the value with object.
We also saw how to delete & update the values.
Next topic will be Inheritance.
Share a personalized message with your friends.