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 fundamentals 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 class is like form or questionnaire. It defines the needed information. After you fill out the form, your specific copy is the 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 people 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 Cell 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 Lu and each step and what does it show the class the instance attribute itself runs when we create this class and gives give it the name of blue and the ages dead.
And in the next object of this class. It gives the value of 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 it rip it is always the same. Now instance method. Now, these are the methods used to get the content of the class and 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're 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 Jedi, friend, jetty run, ready, fast. Let's run this Jedi dance very fast. So, this was the method instance method and we used it for the Jedi 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 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 they compared it is white and white two are different or same It gives false see, it is giving false value. So, that means, each object created from the class 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 get the name of the class sorry, we get the name of the object then dot underscore underscore class dot species 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 we compare two different objects created from the same class and we saw that they are unique and when we are initially 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