Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to talk about the most important module that is called as oops where
we'll be discussing about classes and objects.
Now, object oriented programming is a programming paradigm that uses objects to design application
and computer programs, right?
Oops allows us for modeling real world scenarios using classes and objects.
Now any end to end projects that you are specifically going to create, we really need to use classes.
We need to use objects.
This increases the reusability of the code.
And if I talk about developing real world use cases, we will be able to use it efficiently, like we'll
be able to develop that application efficiently with the help of classes and objects.
Now to give you a basic example of class, let's let's see the definition of class.
So class is nothing but it is a blueprint for creating objects okay.
It will be having separate attributes and separate methods Okay.
Let's say that I go ahead and create a class okay.
So let's give a basic example over here how to create a class.
We'll see this.
So here we use a keyword which is called as class.
And here we will give the class name.
Let's say that I'm going to create a class name which is called as car okay.
Now car is nothing but it is a class okay.
And uh, it is a blueprint of creating objects.
Now I'll show you how to create an object.
Okay, so let's say that this is I'm just writing or I'm creating an empty car.
Right?
Now, when I say creating an object, right?
Car may be of different type.
It may be of different different brand.
Right.
So I may go ahead and create.
Hey, uh, first of all, I'll go ahead and create an Audi object.
Right.
So Audi object, it is a type of car.
So I will go ahead and use the same blueprint and create this particular object.
Right.
Similarly I may go ahead and take a different variety of car.
Let's say this is BMW, and here I may again use this as an uh, or instantiate from this particular
class, right, to create this particular object.
So this is nothing.
But if I go ahead and print what is the type of Audi.
Right.
So you will be able to see that if I execute this it is nothing.
But it is a it is also class.
But this is exactly an object over here, right?
The reason why I'm saying it as an object, because we have instantiated from this specific class,
which is called as car.
Okay.
And similarly, uh, you can just imagine, right.
What class is it is a real world entity.
Okay.
And it is also called as a blueprint.
And just by using this specific class car, I can create different objects which may be using the features
of this specific car.
Right.
Like we have Audi, BMW and all.
Okay.
Now, uh, let me just go ahead and probably explain more about it because I have not still, uh, discussed
about attributes and methods, which we also called it as instance variable or instance methods.
Okay.
So if I probably also go ahead and print this Audi.
So if I go ahead and print Audi what will I get.
It will be showing that hey this is a car object at this specific memory location, okay?
And similarly, if I go ahead and go ahead and print BMW.
Right.
So here also you'll be able to see, yes, it is showing that yes, it is a car object at this specific
memory location.
Right now.
Let me just go ahead and include two more important things.
Let's say I will go ahead and write instance variable and methods okay.
Okay.
Methods.
Now let me just take another class.
Okay.
Let's say that I'm going to create a class called as dog.
Right.
Now dog is a blueprint right.
And in order to define the attributes over here.
Right.
See uh, one way, if I probably take this already and.
All right, if I just go ahead and write ord dot, let's say I want to go ahead and define one instance
variable that is present inside the class.
Let's say Audi has some windows right?
I will just go ahead and write for write.
This windows is basically for right.
And if I go ahead and print Audi dot windows, then I will be able to see that I am able to get the
answer.
As for so, windows is a kind of attribute that is present inside the car, and for Audi it may be that
I may have four windows for some other car.
I may just have two windows.
Like, let's say that I'm taking a very smaller car over there, which is just like a Tata Nano.
Okay, it may be having four windows, but let's say that there is a new variety of car that may have
two windows.
So I can change that particular attribute values.
And this is how we specifically define right.
This is nothing.
But this is a kind of attributes.
But initializing attributes like this is not a proper way.
The reason is very much simple why this is not a proper way.
Because see, whenever I create another object, let's say that I'm going to create another object.
Let's say this will be a Tata car.
And now I will go ahead and initialize with car object.
Right.
I'll go ahead and create this particular object.
Now I may go ahead and write Tata dot doors.
Right.
So this will be my another attribute.
And let's say they are around five four doors over here Right?
But if you see with respect to Audi, I did not created a attribute called a doors.
Whereas over here I created something called as windows in that I did not create an attribute called
as windows.
Right.
So if I go ahead and print tata dot windows, will I get anything.
Let's see.
So here you can see that I'll be getting an error saying that the car object has no attribute windows.
Right.
So defining the attributes in this way is not a proper way.
So I will just show you another way how we can clearly define all the attributes and methods in the
first hand while we are initializing, or while we are creating the objects itself.
Right.
So I'm going to take an example with respect to dog.
Okay.
And here uh, there is one very important function.
See if I go ahead and write dir Directory of this specific class that is called a starter.
Okay.
Here you'll be seeing there will be a lot of different different inbuilt methods right.
And each and every inbuilt methods will be doing something else right now.
Since you can see that I have created an attribute called as doors, you will be also able to see that
this doors will be available over here.
Right inside this particular directory I will be talking about different.
Different uh, over here methods.
Right.
What functionalities it usually does.
But here I'm going to take about I'm going to talk about one method which is called as underscore underscore
init underscore underscore.
Now what this method is all about this method is called as constructor okay constructor.
Now what does this specific method do.
So if I go ahead and write definition underscore underscore init underscore underscore.
If initially I want to define any attributes, I can make sure that with the help of this particular
constructor at the first hand, I'll be defining.
I'll be able to define all the attributes that will be available inside this particular class.
Right now, in order to define it, let's say in dog.
Right.
Um, let's say I have two attributes over here.
One is name and the other one is age.
Right.
So while I'm initializing, or while I am creating the object from a dog class, I have to make sure
that I have to give this two attributes.
Okay.
During the runtime, I have to give this two values during the runtime when I create the object.
Okay, now the best thing over here is that this is basically my parameter, right?
And internally I will go ahead and create some instance variable.
Now this instance variable are like some kind of properties that are available inside the dog class
okay.
And you know that I have to basically take this particular name and assign this name itself, because
during the runtime I'm going to put this particular value.
Now obviously I have written the same name.
The variable name is almost same.
So let me just go ahead and write one more parameter which is called as self okay.
It is not compulsory.
You always need to write self okay.
You can write anything over here.
You can write crush.
You can write your name also.
But self uh is a keyword which we specifically use, you know worldwide in terms of good practices we
use self keyword.
Right now this self keyword will be responsible.
You know, in probably accessing the instance variable inside the class itself whenever we create an
object.
So here what I'm going to write I'm just going to say self dot name.
Now this name that you will be seeing which is which is along with self dot name.
This name is nothing, but it is my instance variable that is available inside the dog class.
Okay.
And this name is what is the parameter that I'm passing while creating the object?
Okay, so here there are two parameters.
One is self dot name and the second one that I'm actually going to go ahead and write.
It is nothing but self dot.
Uh age is equal to age.
So this way I am initializing my entire class with a constructor.
Now if I go ahead and create the object okay, now let's go ahead and create the objects right.
You will be able to see that as soon as I go ahead and write.
Hey, let's go ahead and create my object.
Dog one.
This will get initialized from dog okay.
And here I'm going to give two parameters.
If I execute like this it will tell me hey you're missing two required positional argument name and
age.
Because name and age is not given.
As soon as I call this, what is internally going to happen is that it is going to call this constructor
first.
Okay?
Because of this constructor, it is going to take all these values that is required for the instance
variable.
So here I need to provide two parameter.
Let's say here I will go ahead and write buddy.
And the age is three okay.
Now if I go ahead and execute this print dog one okay.
You will be able to see that I will be able to create a dog object at this specific memory location.
Now this instance variable is their right name and age.
Now let's go ahead and retrieve this dog One dot name and the next one is print dog one dot age.
Right.
So now if I go ahead and access this values, you'll be able to see that I'm able to get that value
which is called as buddy and three.
Right.
Similarly, I can go ahead and create any number of dogs objects.
Right.
So I'll go ahead and write dog two.
And again I will be using dog and I will go and give my parameters over here.
Let's say I will give something like Lucy and the age is four.
And I will be able to print the same thing over here.
That is nothing but dog two dot name and similarly print dog to dot age, right?
So here you will be able to see that I will be able to access this instance variable over here Now the
best thing is that we have defined the instance variable initially in the constructor so that we cannot
create any more, you know, whatever things is defined in the constructor, only those number of instance
variable itself we can create.
If tomorrow I require any more instance variable, I can just go ahead and write self dot that particular
instance variable.
And I'll assign the value based on the parameter that I pass over here.
Right.
So this is the best practice that we specifically use with respect to creating instance variables right.
Now let's talk about instance method right now instance variable whenever we talk about it is some kind
of properties.
That dog that talks about dog right.
So over here name and age are just like my properties for this particular dog.
Right.
It's ah these are nothing.
But these are some kind of attributes.
Right.
Similarly, they may be methods which we also call this as instance methods like a dog may be barking.
Right.
It may be doing some kind of functionalities.
Right.
It may be running at a specific speed something.
Right.
So those are nothing.
But those are basically instance method.
Now let me show you how you can actually go ahead and create the instance methods.
Right.
So now I'm going to define a class with an instance method.
So let's go ahead and define a class with instance methods okay.
Now in order to define a class with instance method again I will go ahead and write class dog.
Okay.
Now as you know that I will be using my init constructor.
So I will go ahead and write definition underscore underscore init underscore underscore.
Here I will use a self keyword.
Let's say that there are two parameters or two features that I'm going to use.
That is name and age.
These are my two instance variable.
I can also say it as properties, attributes, anything you can specifically talk about.
But all these parameters are basically talking about this dog class itself, right?
These are the properties of them.
Right.
And this is what I'm going to pass in the runtime.
Now I'll go ahead and create my properties.
So I'll go ahead and write self dot name is equal to name.
Then I'm going to write self dot age is equal to age Okay, now this is fine.
Right now once I create my constructor.
Now I'm going to write create some instance method that is related to dog.
Let's say the dog will be barking right.
So and different different dog can bark in a different different way.
So here also I'll be using the self keyword.
The reason is very simple.
So that inside this method also I will be able to use this instance variable okay.
That is the reason we provide this self keyword over here.
Now I will go ahead and write print okay some functionality or some uh, some print statement.
I will write over here and I'll go ahead and write self dot name right self dot name basically means
the same attribute.
I'm calling it over here says woof.
Right.
So this is the sound uh, specific dog makes uh in generic.
Okay.
So here you can actually see over here.
Right.
And uh, if I probably, uh, just consider this, this is one of the instance method.
Okay.
Now what I'm going to do I'll go ahead and create my object.
Let's say that this is my dog one.
And here I'm just going to call dog.
And let me just give the values here I will be giving buddy.
Comma three is the age.
Now I can use this object and call this particular function right method right.
This method is nothing, but it is basically called as bark.
Okay, now if I go ahead and just execute this, you'll be able to see that I will be getting that print
says buddy says HUF right.
Now tomorrow I want to go ahead and create my another dog two.
Okay.
And let's say this dog two is basically having the name as Lucy, okay.
And the age is four, right.
If I go ahead and just go ahead and call this dog two dot bark.
So here you will be able to see that I'll be able to see it get Lucy says woof right.
So buddy says woof.
Lucy says woof.
Now this is the function.
And based on different different dog types, you can just consider that the object is nothing but different,
different dog types, right?
They're using the same properties and they're using the same method.
And this will be relative to that specific object.
So that is the reason over here in the definition we have clearly written as a class is a blueprint
for creating objects.
And it has its own attribute and methods which can be used by this objects.
Right.
So I hope you got an idea with respect to this, and I hope you are able to understand this.
Okay.
Now, uh, let's design a better example for you.
Okay.
So here we are going to take this example which is called as modeling.
Modeling a bank account okay.
So now let's go ahead and do this okay.
So here you'll be able to see I will define a class for bank account.
Okay.
First of all, now you know in a bank account you will be able to withdraw money, you will be able
to deposit money.
And you will be also able to see the balance in the money.
Right.
So here, first of all, how do we design this entire application with the help of classes.
So I'm just going to use class with respect to bank account okay.
It is always a good practice that you use capital letter whenever you are defining a class.
Then I'm going to use my constructor.
That is underscore underscore init underscore underscore.
Let's say that what all parameters I'm going to probably give over here.
One is owner information and the next is balance.
By default whenever we create an account the balance will be zero okay.
So by default I'm giving some values over here.
Then I will go ahead and write self dot owner is equal to owner.
So and again guys you may be thinking should this particular attribute be same to this particular parameter
name?
The answer is no right?
You can write as your own, but I'm just following the good practices that many of the developer usually
follows.
Then I'll go ahead and write.
Self dot balance is equal to balance.
Okay, now here you can see that I have created my bank account.
Perfect.
These are my attributes right?
Owner and balance.
We also call this as instance variable.
Now the next thing is that I will go ahead and define my instance method.
So first of all we'll go and create one method which is called as deposit.
And here I'm going to give self.
And one parameter that I will give is amount okay.
Amount is like how much we really need to deposit as the parameter I'm giving okay.
Now whenever I try to add money in my account through deposit, I have to probably increase my balance
now in order to get the excess of the balance again, I have to use self dot balance.
Keyword plus is equal to this specific amount, right?
And this is what we are basically doing with respect to the balance deposit function.
And I will just go ahead and print and go ahead and write.
Hey uh amount is deposited, new balances.
I'm just going to write some print statement.
New balances, self dot balance okay.
Self dot balance I cannot directly use balance because this is nothing.
But it is an instance variable that is available over here okay.
Now the next function that I'm actually going to create is nothing but withdraw.
So I'm going to write withdraw, definition withdraw.
And here I'm going to use self comma amount okay.
And with respect to this if amount is greater than self dot balance okay I'm just saying that hey if
I'm withdrawing, first of all I need to check whether the amount is greater than self dot balance.
If the amount is greater than self dot balance.
And if I am trying to withdraw, then obviously this should not be allowed.
And I'll just go ahead and print.
Hey.
Insufficient funds.
Okay.
And finally I'll close this right now.
If this is not satisfied, then what I will do, I'll go ahead and write my else condition.
And this time I have to subtract it from my balance account.
So I will go ahead and write minus equal to this specific amount.
Okay, so I hope you are able to understand this.
Now if the amount is greater than self dot balance I will say hey, it is insufficient funds.
But if this is not true, if the amount is less than self dot balance, I should be able to withdraw
it.
So here you will be able to see that I am writing self dot balance minus is equal to that specific amount.
And here I will go ahead and print and say that by using an f string say that hey this amount is withdrawn.
Withdrawn okay.
New balance.
New balance is self dot balance right.
So this is perfect.
So I've created two instance method along with that two instance variable.
And finally one more method I will go ahead and create.
And you can create any number of methods.
I will say hey get balance okay.
And here I will again use the self keyword.
And all we have to do over here is that I will just go ahead and write return self dot balance.
I just need to return the balance in my account.
Okay, done.
Now you'll be able to see the balance amount and all.
Everything is there.
Now let's go ahead and create an object.
So I will go ahead and create an object.
Now you can perform any kind of operation that you really want.
So while creating an object I have to make sure that I create an account.
So first of all I will go ahead and write bank account okay which is my class.
And when I call this particular bank account I have to give two attributes that is owner and balance.
Write owner over here is nothing.
But I'll go ahead and say crush.
And initially let's say my balance is I am depositing around ₹500 okay.
Or let's say ₹5,000.
Okay.
Now let me just go ahead and print the count dot balance.
So this is crush account right.
So here we'll be able to see that I'm getting ₹5,000.
Now let's say that I want to call this instance method okay.
Where I want to withdraw some money.
Okay.
So first of all let's go ahead and do some deposit.
Let's say that I got my salary and my salary is ₹100.
And I've deposited it okay.
So if I go ahead and print or if I just go ahead and execute this, you'll be able to see that I'm getting
a message saying that, hey, 100 is deposited, new balance is 5100 right now.
Let me just go ahead and withdraw some money.
Let's say that I want to withdraw account dot withdraw is nothing, but I want to withdraw ₹300.
So I'll just go ahead and execute it.
Now here you can see.
Hey, 300 is withdrawn.
New balance is 4800 right now.
Finally, if I want to get the complete account balance, I should be able to just call print account
dot get balance.
And that is what I'm actually going to execute it.
And here you can see I'm getting 4800.
So here in just front of you I have designed the entire bank account with respect to all the operations
with the help of class, instance variable and instance method.
So I hope you are able to understand this, uh, completely with respect to this particular example
of bank account and all.
Um, as we go ahead, we'll be talking more about different, different examples, right.
This is just to give you an idea of how powerful this class is.
Ah, but please make sure that always make sure you keep a note of all the indentation.
How to probably create it.
Uh, you need to understand very much clearly that this init is nothing, but it is a constructor.
This.
Ah, my instance variable.
This is ah, my instance methods and I can probably go ahead and write any number of instance method
that I want.
And the importance of self keyword is to use this kind of instance variable wherever we want.
Inside this particular instance method, right out of this class, I cannot use self keyword.
That is very much clear, right?
So I hope you are able to understand this, uh, entire video.
So finally I will just write some conclusion saying that What?
All things.
Uh, is important with respect to why this class is are important.
So here object oriented programming is very, very much important.
It allows you to model real world scenarios using class an object.
In this lesson you learn how to create a class in objects, define instance variable and method and
use them to perform various operation.
Right?
So yes, this was it for my side.
I hope you liked this particular video.
I will see you all in the next video.
Thank you.