Hi, and Hello everyone, I hope you're doing well. In this video, we'll be discussing about class and objects in Python. By putting a lot of focus on class and objects in Python, is that this is the fundamental of object oriented programming language. Now we know that class is a blueprint of object. An instance is a copy of class with actual values. Let's put it another way, a classes like form or questioner, it defines the needed information.
After you fill out the form, your specific copy is an instance of the class. You can fill out many copies to create many different instances, but without the form as a guide you can be lost. This before you can create individual instances of an object we must specify what is needed by defining a class. For example, if we create a class of dog or a peacock We must specify what information of peacock we needed. And we can then define different instances of the class like object. For example, peacock one has white color because two has some other color peacock three has another color or maybe they have different ages they have different names.
So, a class defines what information like name age is needed, and the object is the specific actual values of name and an age. Now, a class can have a class attribute. That means same for all the instances. For example, all peacocks must be a bird. However, instance attribute, it is different for each class we use init method to initialize to specify an object's initial attributes by giving them their default values or state. This method must have at least one argument as well as the Radio, which refers to the object itself, how we do it, this is the self variable and what other information do we need?
Name and age. So, the default values of name and age are actually the name and age. However, each object created from this class must have a name and age and that should be given as the parameters when we are creating an object from this class. For example, here, we are using the class peacock that we just created here and we are giving the name new and age 10. And what does it show? The class the instance attribute itself runs when we create this class and it gives give it the name of blue and the ages dead.
And in the next object of this class. It gives the value of one As name and ages 15 So, instance attribute is different for each class. However, it could be same as well, but generally tend to be different However, when we talk about class attribute a tree, it is always the same. Now instance method. Now, these are the methods used to get the content of the class and are generally defined inside class. Actually they are defined in inside class.
So, here we are defining the instance method we are giving it the name run and then we are writing the code inside this definition and calling the instance attribute name. We calling it by self dot name So, whatever object created from the class and and the name given that object should be called using this method Let's try to try this out. We call an instance method. So for object Jerry friend, JD dot run, ready, fast. Let's run this jetty runs very fast. So, this was the method instance method and we used it for the jetty object, which content of the class that we used that we extracted.
It is the self dot name actually the name of the name that is we defined within the object we define the name Jerry. So it used Jerry when it printed similarly, now, there is one more question does each instance is unique or different. For example, we create two instances from the class and we give it the same value like here we created a peacock object from the class peacock and then another big object from the class peacock. It has the same name and the age however just the name of the variable is different via use the names and the give the values in the variable is just to make comparison and when I compared it is white and white two are different are saying It gives false see, it is giving false value. So, that means, each object created from the class hair is unique or we can say that instantiating a class gives a unique instance of the class.
No accessing the class attribute from the class. The syntax is very simple. We use format method that is a predefined method and we give the name of the class sorry we get the name of the object then dot underscore underscore class dots species not species the class instance created within that class so, it will extract the value of Last specie from that class and then put it inside these instead of these curly braces. So, when we run it it will give blue is a bird who is also a bird and you can see that the bird is actually defined within the class as class attribute. Okay, in this video we actually discussed classes objects, class attribute, instance attribute, instant method. And we discussed this with the example be compared to different objects created from the same class and we saw that they are unique and when we are initiate instantiating any object from the class We are actually creating a unique object.
So, in the next upcoming video, we'll be discussing inheritance within Python. Thank you