Python Tutorial for Beginners 15 - Classes and Self


 hello guys in this blog we are going to learn about creating classes and using classes in Python now we all know that Python is an object-oriented programming language and in object-oriented programming language a class is an extensible program code template for creating object providing initial values of States and implementations of behavior now this definition of class you may think that you know what is this so let me give you an example suppose you want to create a class or you want to save some data for a person now person has some attribute for example first name last name and for example you want to print the person's name so a class can also have member functions or member methods right so we are going to take this example of person and we are going to create a person class and we are going to give the attribute to this class as first name and last name and we will try to print the person's name okay so let's see how we can create a class in Python now in Python the class is created by using the word class itself and then you give the name to the clasp in our case we want to give the name to the class s person so we will give this person name to the class and then this colon comes and inside this class you can define member functions or member variables now how you can define member functions in a class you just need to give for example I want to print the name of first name and last name of the person I can create a I can create a method to print this so I can create a function like this print full name for example this is the function name but we all know that function whenever we want to define a function we use def in front of a function right and then we can pass some argument here now whenever we use a function inside a class the first argument we give to the member function is a keyword called self and this self is nothing but a pointer or the self points to the class itself okay so this class whenever we create an instance of a class which I am going to show you how you can create an instance or object of a class this self point to the object of the class so just wait for a while and you will understand and then you pass the name of the argument so for example first name and last name okay then colon and then you can give the definition to the class so for example I want to set the value of first name and last name so what I can do is I can use the same keyword self to call the first name variable and last name variable of the class this should be last right and then we use a dot separator to call them so for example you want to set the first name we just use self and first name is equal to first name and then in a similar way we can just define the second variable or second setting the second variable which is last name so self dot last name is equal to last name okay it's better to rename this function as set fully named okay so what this function is doing its setting the first name and last name to the variables in a class or member of variables in the class which are first name and last name for whatever argument you pass here first name and last name will be passed to the member variable of the class which are also first name and last name okay now we have already created this class and now we are going to create a second member function to print this first name and last name a full name okay so we will define the second function called print full name and this will not take any argument and we are just going to return or we are just going to print the first name and last name okay so we are going to print here first name so to call any member function or member variable of a class you just need to use self and self just indicates that it's a member variable or member of a class okay so always use self and we going to print a self dot first name which is the first name and then we can just give a little space here to separate them first name and last name and then you can just take the last name and print it now in order to create an instance of a class so how you can create an instance of a class this is just a definition of the class right we have done defined the definition of the class right now in order to create the instance of a class so that we can use this class we can use this class by creating the instance of the class so whenever you want to end the class and you want to execute in this class just remember that you start from the corner here right so and don't forget about indentation in this class declaration when you start with no indentation here then you are in the new line and you are outside the class okay now in order to create a instance of the class or object of the class we can just define the object name for example person name so this is the object name or instance name of a class we want to give and then is equal to the name of the class and then this these brackets okay and this will create the object of person class that a named person name okay so let's try to run the program and let's see if it contains any error or not save okay and it doesn't contain any error so it's running fine but we want to use the member function of the so how we can use this member function of a class we can use the member function of a class using this instance of the class or object of the class okay so just take the object of the class and in the next line we just copy the object of the class dot and then we can see there are two options which are member functions of the class print full name and set name so I'm going to take set name set name of the class function so this is the member function of a person class and we are accessing this member function using the instance or object of the class okay and in between we give dot separator so that now this instance can call the member function so member variable of the class and now we can set the first name and last name for example programming is the first name and knowledge is the last name okay so we have set the first name and last name of the person and just remember that this is used as a default so this will be automatically taken by by your class and these are the two important you know two important argument which you need to pass and this will be taken automatically okay so self is by default for a member function invisible in a sense that we you just need to pass the other arguments except for the self argument so first name we passed as programming and last name we pass test knowledge so this is setting the first name and last name of a person now in order to print this name we can once again use the instance or object of a class dot we can call the second function of a class called print name which takes no all argument right and now let's try to save the program and run this so run this and what it's saying it's saying that this member function which you are calling which is print full name doesn't have any arguments which is zero argument right takes zero argument but one was given right so I intentionally leave this self in this function so that we can see this error so always remember you have to at least pass self whenever you define a member function in a class so if even if it doesn't take any parameter or argument you need to pass self to a member function okay now when I save the program and run it it will run fine right it prints the name programming space knowledge which I wanted to print right so always remember few things whenever you define a member function in a class always use self as a first argument and then give your rest of the argument and even if your function or member function doesn't take any argument you just or you must pass at least one argument which is self okay once what is self self is a pointer to the class instance so this self is pointing to this person name and this is called class instance and whenever we create the class instance it will assign this person here at the place of self and then it will be named will be set as person name which is this instance dot first name person name dot last name and then whenever we call this function it will print person name dot first name person name dot last name so in this way you can use or you can create classes in Python there is lot more in defining and creating or using classes in Python this is just starting or create how to create a class in Python so I will show you how to use other functionalities in class in later blogs and hopefully you understood the concept of the class and hopefully you enjoyed this blog 

Post a Comment

أحدث أقدم

Recent in Technology News