CLASSES & OBJECTS - PYTHON PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] her friends welcome back to our channel so in the previous session we have seen the object-oriented concepts introduction in Python program so whatever the language it may be the concept is same so object-oriented concept so we have seen that all the interactions about this class object abstraction encapsulation and inheritance and polymorphism so today's session will go with the class and object so how to create a class and how to create an object like so a class can be having both the attributes and the behavior so that we call it as data and functions so class can have the data and functions so both the data and functions will be combined and called as a class and these two are called as class members so how to access these class members how to access these class members that means in the data our functions so how to access the data or how to access the functions of a class so that can be done access with the help of project with the help of object so if you create an object with the help of that object we can access the data and functions of that particular class so this object is reference to this class reference to this case so once a class is defined with a data and function an object is created as a reference to the class so that object with the help of this object we can access the class members right access it by an object so let us define here so let it be as having a sorry so let us take a student as a class a student class is having our member name and a branch so three attributes or simply we can call them as three variables or the data okay the data and functions we can use a real function is the wave function right so this is called a class and for this in order to access this one we have to create an object so how to create a so these are the data and these are the functions we know that the functions always starts with in DEA of keyword the same very giving like so a class can be represented in Python as a class so it should be in the tasks should be defined by using the cache keyword so cache followed by the class main it's a lot of statement so this is the data and functions function 1 function 2 and so on so this is how we can represent a class in Python so here in Python programming a class is a block of strengthened class represents a block of statement so here we should write both the data and functions right see another is students cares so it has student block of statements or indentation then here we can write the data our number is equal to sum 1 2 3 some name is equal to some ABC some branch is equal to C AC so what all the data we can write here so and then functions so we know that the functions user-defined functions but these are implemented in a class so every function should start with the DF keyboard so do you have the function named solely foreign because it's a block you're just I'm writing only the print statement so in writing the print statement so similarly B if write function print writing so this is how we can create a class so outside the task that means after anything the block of statements outside the class now if you want to access these data or functions they have to create an object and see every one more thing should be remembered that every function the arguments list argument list starts with self self so self is an implicit instance so self is an implicit instance or implicit reference so if you know Java the this keyword can be replaced with the self the working of this is nothing but our self so always the user-defined functions which we are writing inside the class the argument should be self so it is a mandatory for every function so if you are not writing this self as a parameter to the user-defined function definitely we will get F ever because it will tree the first argument as a self and it is a bandit so here you have to write it as self so if if our use a different flow we function doesn't have any arguments then only one argument will be there that is self if our loser different function is having two arguments then the first argument will be the self and the second argument will be some other variable a and the third argument will be C so self is a mandatory self is a mandatory and that's an or that that must be the first argument in the argument list right and now this is how we can create a class this is how we can create a class now the question is how can we access these variables and these functions and for accessing this one what we have to do we have to create an object so here the syntax for object is so whenever you write this class the object will not be initialized until you create the object so object is the creation syntax is object name is equal to class main Kansas caseinate bounces so here object name is equal to class name parentheses here the object name is an identifier so we have to follow the rules of an ID in different people giving the name for an object right that will be am giving here some ABC is equal to student MBC is equal to student so ABC is an object or simply we can write it as some s1 okay s1 is equal to student s1 is equal to student that was our student 1 so s 1 is an object first student and similarly some s 2 is equal to student so we yesterday in the session I have said that one class can have a multiple instances number of instances right so similarly one student cash can have multiple objects so here I am creating two objects s1 and s2 so if you want to access this data in functions of a class simply we have to access with the help of the object name so yes one dot R member will give the given number similarly yes well dot name will give the name which is given in the class similarly a small dot branch will give you the branch value here and these are called instance variables instance variables so if you are creating the variables inside the function those those variables will be called as local variables okay so here the variables are not a local variables these variables are defined inside the class but not in the function so that's a important thing you have to remember so variables which are declared inside the class but not inside the function those variables are called as instance variables and that can be accessed only with the help of object name or the class name so we can also or we can also access with a class name student dot our number similary student dot name similarly student dot branch so in any ways we can access them data similarly if you want to access the function we have to write s 1 dot read yes all dot right so if the function count we need not specify any parameter but inside the function definition you have to write the self parameter and similarly here also we can access these functions with the help of student name so the class name so that is a student what real student dot right on s1 not really not an X or not so these members can be accessible with the help of object on this task name right and if there if there are local variables that are variables which are declared inside the function those variables can be directly accessed ok only the instance variables that means a variable so just remember the variables which are defined inside the class outside the function are called as instance variables these key instance variables can be accessed only with the help of object name or a class name so this is all about our ascend objects so how to create a class and how to create an object so a class can have a data and a function both we call it as a class members so in order to access those members we have to create an object so object creations index is this one object name is equal to class name per once's so one caste can have a multiple instances right so we'll stop here and we'll see the implementation so how to create an object and how to create a gas and how to access the data members I mean class members that is a data and functions so we'll move on to the interpreters hi friends so just now we have seen how to create a class and how to create an object for that class and how to create variables and the functions so we we have said that a class can have a data and a functions right so how to create that one let us see this one so we have to begin it class with the class keyword so class followed by the class name so let us take student as a class name so a class is a block of statement so which contains the variables and functions so student so here we can give student variables different variables so let us take some some branch is equal to CAC and I am writing the function DG of so in Python every function uses a different function should start with the DF keyword so d EF so read so I am writing the read function so a student can read okay student a student will be having some branch roll number name and so on so our number is equal to 1 2 3 and similarly name is equal to sum ABC right so read function so read just I will write a simple print reading right so let us close this so this is a class so which can have the variables as well as functions so we can write a multiple functions here so in a class can have a multiple functions and multiple variables now in order to access the function of this class we'll need n objects so without creating an object we can't access these variables or these functions inside the main function so here I am creating an object some ABC is equal to so this is the syntax for creating the object so ABC is equal to student now ABC is an object for the class student so in order to access the objects or variables of a student that should be accessed with the help of object name so print ABC our number is equal to object name dot our number so without using the object name it true it cannot be printed right similarly print name is equal to name print Branch is equal to match and if you want to access the function here that also should be done with the help of ABC ABC dot sorry here I didn't access these numbers using the project so once again I am repeating so if you want to access the class members and that should be done with the help of the object created for particular class so here ABC is an object created for student cast so in order to access the variables and a functions available in that student class that should be done with the help of this object ABC and a class can have a multiple objects right so I will just save this one so here I am saving that one so student class dot pu5 right I will run this one so you can get this our number ABC and the branch and here you can observe that ABC dot Reed is having an error that read function takes a zero positional arguments but one was given so why because here if if you are writing any function or if you are defining any function inside the class one argument is compulsory that is a self self is the first in the argument list self represents the reference or the instance of the same class instance of same class so we have to pass self as an argument for the function which we are writing in the class itself right so the parameter list will start with the self so if you are writing two parameters first we have to start with yourself self comma a comma B right so self is a mandatory now you can observe if you run this one so you will get the result that our number name branch and ready so if you are trying to access without the class name what happens so you will get the error right because name our number is not defined so here our number is not defined because our number is the variable declared inside the class so it's an instance variable or a class variable so every class variable or an instance variable can be accessed only with the help of the object name or a classmate so you can observe see our number is equal to 123 so an instance variables can be accessed through the object name or a class name so once we have to create an object and then we have to access with the object name or a class name right right and now see if we are having the same thing 3 4 5 and here if you are trying to print our number see already we know that the user-defined function may also have the variables right not only the class user-defined function may also have the variables so these variables which are declared inside the class in the out style to the function those variables we call it as class variables or instance variable so these variables should be accessed only with the help of function I mean class name or object and whatever variables we are declaring inside the function of a class those are called a local variables so they can be directly used in that function itself so if you here see observe so I am taking a different name so roll number is equal to 345 that's a a variable declared initialized inside the function I am printing the same thing roll number so you can observe here so even though we are executing so we can get that our number is equal to 1 to 23 name is equal to ABC branch is equal to z AZ + 345 here we are getting right so right now if so these are the instance variables and this is the local variable right now if the local variable and the instance variables are sharing the same name the first preference will be given to the function C so here only the our number so whatever the variable we are right so whatever the variable we are declaring inside the function that will be displayed here so you can observe here so our number is equal to 123 name is equal to ABC branch is equal to CAC and roll number is equal to 345 this is the this is the thing we have written inside the main function inside the read function and if you want to access the instance variable inside the function then that can be done with the help of self or number is equal to self sorry here I will write here instance variable is equal to self dot our number so self is nothing but the implicit reference implicit reference or implicit instance so self without creating an object we can use the instance variables with the help of self so self dot our number so this will give the instance variable see if you can observe that see one roll number is equal to 1 2 3 name is equal to ABC branch is equal to ca ca here roll number is 30 45 and instance variable is 1 2 3 because here we are giving self dot roll number which is nothing but 1 2 self dot self is an implicit instance variable implicit instance of a class right so all the variables which we are declared inside the class can be used with the help of self inside the function and outside the function outside the class if you want to access the variables and functions that should be done with the help of the object so we class can have a multiple objects so with the help of the object name or a class name we can access the class members here the class members are instance variables and the functions so a class can have a multiple variables or multiple data and also the multiple functions so we can write a multiple functions right I will write a one more function the EF and one more thing every function should have a single parameter that is a self self is an implicit instance right if you want to access this side function you have to use ABC dot right so with the help of object you can observe here reading and writing and here you can give a one more thing from d EF sorry d EF is a keyword we should not use it as an object name so some B let us take this one obj is equal to student so it is nothing but the second object it's a second object for the same class so again we can access this one okay so hope you understood this one so this is how we can create an object and how we can create the class so if you are having any doubts regarding this class and all object for feel free to post your doubts in the comment section so that definitely I will try to clarify all your talks if you really understood my sessions like my sessions share my sessions and don't forget to subscribe to our channel thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 72,757
Rating: 4.9127274 out of 5
Keywords: sundeep, saradhi, kanthety, java basics, beginners, programming fundamentals, source code, object oriented, oops, oop concepts, abstraction, inheritance, polymorphism, encapsulation, class, object, portable, robust, multithreading, method overloading, method overriding, oops example, PYTHON PROGRAMMING, PYTHON FUNDAMENTALS, interpreter, python applications, class creation, object creation, self, class members, data, attributed, functions, methods
Id: hLC6GL_YBGM
Channel Id: undefined
Length: 25min 23sec (1523 seconds)
Published: Thu Sep 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.