An Understanding of Objects and Classes with details

8 minutes
Share the link to this page
Copied
  Completed
An Understanding of Class and Objects, Syntax of Class and Its components and Examples.

Transcript

Hi and Hello everyone. This is the second video of the series objects and classes in Python. In the earlier video, we discussed about the topics to be covered in this series. For example, object oriented programming in Python, class inheritance, multiple inheritance, and operator overlapping. We also discussed the programming approach in Python that is discussed about object oriented programming, and also gave you a few details about how that is executed in Python. In this video, we'll be discussing more about class and objects.

What is a class, a class has all the details of the object whether it would be attribute or behavior. Generally attributes are those things that remain same. For example, for example, if we create a class about the character, one of the attribute could be animal, this attribute would remain same for all the objects that we created from this Class of cat because the cat would not like would not be anything other than an animal. However, there could be different methods and different other attributes that could be changing for the class cat. Now, that is, for example, decades, they would have different names, they would have different colors, they might have different running speed, and they might have different behavior towards different people. So, these would be different things that the class would have.

But as you saw that, there could be like different types of attributes, one attribute is the class attribute that is going to remain same throughout all the objects that we create from the class. So, coming back to the class, how would you distinguish between an object and an A class? Now the class is the blueprint of the object, it has all the descriptions of the object that would be created from the class. Now, what is an object? An object is the instantiation of the class. So many Create a class we are actually defining what would be the details of the description that would be needed for the objects that would be created from the class and when we create the object what we are doing is just defining the names and the values of the variables or the attributes and methods for that we defined in the data we gave in the class and we are just giving it when we when we are creating the object.

So what we do actually, when we are creating object, for example, we created a class at and when we are creating object we gave, get one, get to get three. For example, if it had a different name to the object would be like Tom, the other name could be like Johnson, or anything that you would like to give the name of the object. hope not. Now you've got some idea of what the classes and what the object in class what the syntax is going to be. The class syntax is very simple. We name we use the keyword class and the name of the class that could be anything And then there are two round brackets and then a colon the past year you know that the difference between passing a comment comment or non executable period as the passive executable however, generally it when the past is used to give when when when we are going to write the code for it, but at the moment we have not written any code we use pass an instance attribute which is discuss the instance attribute that is it is going to remain same throughout all the objects that are created from the class and after that now, in method there are actually two different types of methods.

One is the method that is executed as soon as we just use that object created from the class. For example, I created this method def in itself and print nice we have get now what will happen whenever we need to now here I created an object Dom with this class, named get Okay. Now, when I go I'm going to run this code Ctrl Enter, it will print and execute this method. This is called constructor method. What happens is constructor method execute as soon as the class or the object that we created from the class is executed. So it's surf executed surf.

However, if we create another method for example, here I have created another method. That is def run. Okay? It has a different syntax. Now the in it is keyword in it here is also a keyword. However, here, I use a custom method name that is done.

So it is not going to execute itself. Let's see this. Okay, all right. Create Jerry. Because no matter where I'm going to execute this object, okay, this gave me an error because Okay. Okay, it requires a name and an age, I just removed that.

And instead of these two, just to keep things simple, I just execute. Okay? So I'm going to run this, okay, this is the constructor meta depth executes See? So it executed itself directly when I instantiate the object from the class. Now let's revise this again class attribute is going to remain same for all the objects of the class. For example, in this case, it's a bird.

In the above case it was the animal that is meant to remain safe. without all the objects objects created from the class, and instance attribute data, according to the instance of the object that is created, it means it is going to be changing. For example, here, the name and age would be changing for each object created from the class. And the constructor method itself executes when the task when the object is executed. So here, let's remove this. Now let's see this example.

Here. What we are doing, we just created a class peacock, the species is bird. And then we created a constructor method with the serve, comma, name, comma, age. So we have to define the name and age. These are the parameters required, and we defined it self dot name because to name and self dot age equals to age. So what the user is required to do when the user creates an object from this class because it gives the name and the ID So, when these two objects are created, and when you want to run this, we can access this method, I just use the method run to print out.

This it will return it will return self dot name that is blue and it will use the method run. So it printed, blue runs happily. If you want to use the name and the heck x is the name and an end age, you can print because name is here. We justified the put the curly braces to use the name that we defined in the object and then the age And it printed exactly the same thing. So this is how we access the instance attribute and the class attribute. In the next video, we'll be discussing further about these classes and objects.

I hope you enjoyed the video may have any questions and queries feel free to contact and I would be glad to answer your queries. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.