The instructor masterfully strips away the academic pretense of OOP to deliver a pragmatic guide that even seasoned developers will find refreshing. It is a rare instance where technical depth doesn't come at the expense of clarity.
深度探索
先修知识
- 暂无数据。
后续步骤
- 暂无数据。
深度探索
classmethods本站添加:
Okay. Uh we were talking about uh object- oriented programming and we are coming to the final of it.
Uh uh and we have created classes and objects and methods. So today we'll talk about class methods. So far we have seen only about the we have only seen about the instance method or the object methods.
So and a method like this and I have told you that methods are just functions that are attached to object or a class.
So this method now can only be it can only be accessed by by the instance or the object only if there is someone who is un muted just mute.
Just mute. Okay.
uh this now is an method that belongs to the instance meaning object that we have created an access. So like here we are having uh we can say uh if we say like print uh student one dot and we access the method like it was display self.
So it was display self like that. So you see that this object that is student one is accessing display self method. So by that you will see what it will you will see it will return the name object which is student one name is you will see that it will return that object's name like you see it's returning abd together now but if we say let's say uh we are having this class which is student if you say this student will access that method let's see so we will say student which is the class dot display self and let's see what will happen. So if I run you will see you will get into an error. So you see what let me uh let me minimize this one so that doesn't confuse you.
Okay.
How you see student display self missing or require positional agreement. So this means what? That the class >> um guys.
>> Yes.
>> Mentor.
>> Yes.
>> Um someone someone is requesting to change the theme >> say this online still. So everyone the font font.
So lambda change the font.
Yeah.
>> Okay.
The problem with this is like you will not see everything. We have to scroll every time. Okay. No problem. Now you see that I have said the class which is the student dot display self and we are seeing what an error student missing one required position argument self. So you see that it was expecting a self the method was expecting what a self argument meaning an object. So these methods are only can only be accessed by the object or the instance. So today we learn about what we call class methods.
class method. Class methods are just methods that a class can access and even the class method as you will see even the object can access but this is a way that a class can access a method. So let's now see how we can create. So let let me remove uh this one so that we will not see that error every time we run the course.
Now we'll start by creating uh we'll start by creating the class method. So let's see how we can create a class method. Remember that a class method will be attached to the class like this class which is a student want to have a method like a function so that you say student do that method. So let's see how we can create a class method. So you can create a class method. First have to indent meaning it should be inside the the class like the rest of the method. How do you know that this method is a class method? So you have to start with what a decorator called class method. So this is a default default decorator called class method that is provided that is provided by.
So immediately you see this decorator decorator we said is a function that adds an extra behavior to a function meaning immediately we place this decorator above a function. It will know that this function belongs and can be accessed by a class also it can be accessed by by the instances. So let's see now let's create the function the normal method.
So we can say uh let's say uh we want to update this name. Remember this we said that this is what a class attribute and we have seen that it it can be accessed by both the class and the object. See how they relate. The class attribute can be accessed by the class and also the object also. The class method is like that. it can be accessed by the uh class and also by the object. So let's see let's say I want to update this attribute class attribute university which was uh which which was belonging to the world class object and the class.
So I want to change I want to create a method that can update what this class attribute. So I can say uh def update university I can say update university or change university whatever you want. Now look now for the methods like which was belonging to the object only we are having what a parameter self we have seen what self represent self is said is the current object. So every now like this it contains self this method contain self all these methods contain self. So we knew that when uh and the self parameter was replaced by what the current object every time. But now look inside the the class method. Now the parameter will be class like just just like the self. So cls in class meaning when we access this method we say student update university.
So class is just a parameter. it will change to what student. So we'll say now let's say uh then add another parameter let's say new university can say new university and then close it and then enter the body function.
Now this function it's a class method.
It what class as the first argument and also the second argument is the new university name then the name that we want to change from this university. So we can say what now we can say the because we want to change this. So for us to for this student class to access this university and we can get this value or this uh we can access this variable we can say what the class dot university. So we will say what we can say class the current class which is the student do university will be equal to what the new university like this the new university meaning that every time the class which is the student will call uh this method which is the class method class will change to what it's students class student and then new university is just a parameter that we will pass to this function. When we call when we are calling this function have to pass what new name. So this new university will become what the name like if you pass university or MK university it will be like that then this means what the class meaning the current class which is student. So student university access this variable or this attribute. Now before it was G university. Now we are saying what is equal to the new university. We will pass.
Now we can just say uh for us to see let's say we can say print or we can just say print university name.
You can just say print and then uh we format university name or the university updated the new name or the new university like that.
So here we're just printing for even uh if you want to see what the class entails you can just say also print and then you can say format class you can say is then we access what the cls this one is just for you to see cls to which means the class it contains what because it's just a parameter it will be replaced so to avoid this multiple bins because we also will print. We can just say what we can remove this paint and we can say just return. We can use just return and then we use the format simple like that.
So look now this is what class method that the class student can access and it's doing what it's updating this class attribute which is now GIS university.
So now for now for us to access we can do what? For us to see what's happening inside here we can do what? Now we can say print. We can just say print and then we can say students which is the class but and then we say the the method was what? The method was update university.
So we can say update university. So the method was this method was having what?
This method was having two parameters.
So this initial you don't have to pass.
It's just like what the self it's like the self meaning right now I'm calling the method I am saying student update university so this is student class immediately it calls this update university this cls of the class will be what will be student this one is passed automatically don't have to pass an argument so what you have to pass is this one right now new university which is a parameter now you have to do what you have to pass a read argument that can override that one. So you can say university like that.
So immediately now look at the process just the normal understanding if you have understood the functions this one will be simpler. So students which is the class dot update university call update university method. So it will check if the method a class method first it will do what it will show the error this one missing one required like that but right now we are having what the class method decorator this one is just to tell this method can belong what or can be accessed by the class this a class method meaning this is uh this belongs to the class so look at it now and now the function after we have value that we have passed me university. So CLS will be what? Will be student. Then new university will be what? New university now will become me university. CLS was the student. So the parameter was replaced by student. So student class university meaning this class which is the student university.
So the initially it was GI University.
But here you are seeing what it's equal to new university and new university.
So here you see this one it will just show you for is just for you. I want you to to show you so that you can see this cls was what contains the class. So here we are just saying return university updated to new university and new university was what was Mary University.
So let's see the output if it matches whatever. So you see first now the output this is the first line.
So remember is coming from all of this.
So student.update university. So it will call what after it called this update university first print it's saying what you look now you see cls let's say class. So you see pass let run again. So you see class is what? pass main student meaning the current file this is not script so it means the current file dot student meaning that cls say to owner it's what student so every time it's called the function will be student so then after that you see what return name so you see university what university so if you want to look still You can just say what after update look you can see print student dot it was university so you can say student do university and you see and let's look what it is so you see student after we have printed student university you see what university so it means that you say this class is student university. So university was holding what Garis University but at the print you are seeing what Mary University why because you have already updated using this class method. So that was that is what an example of a class method. So the class can access. So let's now do another example.
So let's say we want to count all the object or right now the object are the students. So let's say we want to count all the students that are inside uh this class also and also we want to like get all the the the list we want to loop through all the student and get their list. So we can create different method for that.
So let's say now let's start with this.
So we can say uh let's group all the students in a list.
So this just what we have learned.
So we will just uh it's just accumulating. So list we have learned already. So let's now organize all the student list. Remember what I want to do is I want to add like uh I want to count how many object are in this class.
So for me to do that I need class methods because what now the class is only what it know what the class can know what it object like it's a parent and a children. So what we will know the number of instances or the object by using what the class. So here we are having now all the students we have grouped what into an empty class again we can create uh remember the main goal that we want to do right now is just to track how many objects or so far and our object is what our objects are the students which are inside this class the actual students. So our object currently are these ones student one and student two. So you see so far we have what only two object. So we want since the object are controlled by the class want to track how many students are in this class. So we have created a empty list that first will hold all the student in an empty list and then we can create another variable that can hold the total students.
So let's say the total student are zero so far because and it will always start from zero. So you can see now the total student are zero. So we we will leave those variables at the top. So next we will come now to create the class method that will do all of these. So for us now to have the the list of all the object inside this list first we have to register them or add them this work to this list. So we need to add every time we are initializing the attributes of the object or we are creating the object.
Every time we create an object we want to do what? Every time we create an object, we want to append to this list.
Again, another object is created.
Student two, we want to do what? We want to append this list. Every time we want to append to this list again, let's say uh now after we have we append to the list, now we can do what? Now we'll have what?
like student one, student two like that. So when you have an object like this now we can do what we can count how many are they we can loop and then count. So let's leave now this one as an empty list then we will append each time we will add we will append to this list.
So let's now create a class method. At the top here we can create a class method. So now for us to create a class method we have said we start with what?
We start with what? When we are creating a class method please someone to answer so that I I see if if you are with me.
We start with what? How do you know if a class How do you know?
Hello.
>> Are you talking?
>> How do you know?
Are you guys there?
Hello.
Yeah, I want to talk.
So I want to create another class method. So we said how do you know that this a class method or when how how do you start?
Don't understand someone answer.
Someone answer please don't waste time.
Hello.
>> Are you guys not hearing me?
Because if no one with you, we >> just say to like what what what do you know like what do we start with when you create a class method?
>> Selfh >> self >> self. Are you sure I want to create a class method? How do we set class at class method?
>> So let's start with at class method. So we start with class method decorator.
So this decorator now will tell that this is what?
So this decorator will tell that this uh this method belongs to this class uh to the class. So now we can create a method. Let's say this method will add the register student.
We'll add for register student. And then now we will have what for the first default parameter we'll have the class meaning the current class which is accessing this function or method. And then next parameter we can have is student because we want we want each and every time the student call this function we want to do what we want to add a student. So we will have what student meaning the current student or the so to the object. So student and then we can say what student remember here we were having what empty list.
I want every time we create an object to append it here to add here again another one another one like that. So the class which was the cls was was what student ma it was containing the student. So we will say what class dot all students meaning all students is what is an empty list and we have seen that one of the methods of list was what append it was appending something adding to the last. So we can say class dot all student because all student was what was like a class attribute that was belonging to all class. So we can say class dot all students which was this variable class do all students then we say what do append meaning to the last position and then every time we add what student meaning the student this student parameter student object every time we create an object we append what to the old student variable like that and then we can just Say every time remember we also want to track so we have started tracking from zero every time we add we also have to account student one okay I've created the first object is student one so immediately I add student one I have to do what student should be one like that again another object student two the second object. So this one have to upate two like that. So we are we want something like that. So let's now come back to our function. So for us to append we are having the list. So we have said what append meaning to the last. So every time it's being added here then we can say what for us now to increment this number we can say what the can access this variable or this attribute.
So we can say plus which is the student plus dot student which was this we can say + one meaning plus is equal to one meaning every time you have to this one also we have talked about it was this one we also talked about it was an assignment operator meaning add one every time you add an object add one like that simple and we have discussed it many times Then uh for us now to make sure that this is possible what we have to do is is to is what is we call this method together this method and this method we want it to be called when automatically every time an object is created. So when we create an object and remember we said when we create an object like this create object what will happen is that this init constructor method or the d method will be called automatically and the self will be the that object and this init method will do what will initialize all this attribute. So we have to make sure in this stage we call this class method which was register student. So we'll come here and then we can say what student which is the class register student meaning access that one student and we self meaning the client object. So look now we are not running the function from the bottom we are just running inside here meaning now let's say look at the process you have created this object.
So after you have created you know that this init method will be called automatically to initialize the attribute. So student one name like that student name which was like for student like that. So now initialize the attribute also here you are seeing what student meaning this class student register student this class method. So we have the class method the class can access this method. So we can say the register student and this now the class will be what that student class have it the class w just even you can see student. So the class is just a parameter student. Then uh student will be what? Remember look you have called the function student and it was having what two parameters and you have pass what one parameter which is self. Self it is the current object like what the current object student I have a lot of times student one. So here will be what this student with an argument of the current object which is student one. So it will call this method the class method. So classing because student register and student was the class.
So it class will be student with capital S. So it student so this one is passed automatically. You don't have to you don't have to pass an argument for it.
So again here we are having what another parameter student. So this parameter and now we have provided an argument meaning the current object self one student one and then class class was what student class all student this one this list this list append the last position append student was what student one object. So it will do what? It will add a student one to this list. So if I type what student one as simple as that again the next line.
So class is what student capital the class student this one this variable here then you are saying what you see that initially it will it is zero. So but here you said what the total student variable has equal meaning add one every time you add an object. So what one simple so we don't want it like that but that is the process that will happen.
So again uh we can now for us to get now and see like uh list or the count we can say what create another method which so let's create another class method.
So we can say at class method class method which is this and then we can say student.
So we can say count the student and we pass the class as the parameter and then we can say class dot student meaning this function is just returning whatever is inside because here we have incremented we have updated attacker to like whatever is it's holding this so that we track how many students How many object are we together? So this function or the this class method will do what will return the total number of student. Again we can add another class method that will list so that we see all the object that we list all the objects.
So we can say at class method at class method and then we can say list all student then we can say let me make it space.
So you can say list all students and then we pass the class as a parameter meaning the current class that is accessing uh this method. So how now can we list all the student?
How can we list all the student? We have to do what? We have to loop we have to loop through the list so that way we can see every object like if this list was holding uh student one student two student like that. So we want to loop like for for for every time we look we take this student we take this another student like that. So we want to see all the student we want to list them one by one.
So we can say we are having this list method. So we can say inside this method for student. So this for student for loop we have discussed a long time. We don't have to repeat again this student now is just a variable that will hold what? Every time it loop it will hold a student or an object of the class. every time it look in a object.
So the variable will hold will not hold a permanent uh value. So it will change temporarily. So we can say for student we loop through this list to this list here which is will contain all the classes. So we can say for student in class do all student. So class student was holding what class in a man in CS that it means this class the class remember the class access that method so it student class so student all student will have what this list we have started from empty but we'll see that every time we create an object it will add into this list so what we want to look through is this list that is appears here. So we'll do what we are inside here. So we say for now every time a student in the current uh in in all student list we can say what uh we can print their name so that we see so we can say int uh student do name or we can format let's format and we say student a variable to say student name like that. So this class method now we call by what?
By the class student. So when the class student list it will not provide what an argument has this parameter class that specific student.
So again we loop for each time we loop through this list. We'll see what we will we can access everything or every object inside the list and then here we are just doing what we are showing in the console that specific uh object dot name. So we'll see that specific name and its name. So let's see how now how we can access this method. So let's see how we can access this methods.
Uh this was the first method that they have created. So this one was just appending or registering new student and this one was just counting meaning it will return whatever is inside this variable and that one was just listing.
So how can we now see what they are all doing. So let's see now let's say counts let's call this method this method which is a class method. So if a method is a class method it means it can be accessed by this class which is a student also it can be accessed by object but now we are targeting only the classes. So we can say let's say here you can say print and student which was the class but was count was it count it was count student.
So we can say uh student dot count student dot count student and we call that method. So this class which is student is accessing what this count students method. So it will call this class method which appears here and it work is doing what? Okay, it's having a parameter class meaning class that student count. So who is calling this countless method? This class which is the student. So in a mania when it calls this when it calls now this uh this method here this class parameter will be what student like this method so just just that's just a parameter it will be replaced automatically like just similar to the self how it works so it's same and student total student total student was just class attribute. So it's just saying this student.
So here we are having zero but to my final processing in we have said every time we create an object like this student two or an object like this student one append to what to the student list and also implement what this count. So you might you might expect to see the zero but if everything works correctly you will not see. So you will see what every object like say you going to one object another object. So you will see what two objects when you call the student method let me comment this once so that we see only the result that we are we intend to see.
Okay, let's now run our code and see what we expect. Now you see we are seeing the result. So you see it's printing what? Two. Why? Because we are having what? Two objects. So if we create another object, it will be automatically be three. How? Because this class me this class which is student it has access what this count student mess and it was doing what it was returning this student which is appearing here but you expecting what zero but you see what why because look at the init constructor say what every time look let's see the process let me repeat again So this time you have created the object mema okay student one an actual object to student class you have provided this as the values or the real attribute. So it does what immediately you create like that the same the init method which is a special method will will call automatically to initialize the attribute and the self student one the bottom which was the current object the name everything will be passed but look here we have added what a new line meaning student which is the class both register students or register It's where it's made this method and you see at the top of it is a class method meaning it can be accessed by the class.
So you see student register the student meaning we are calling this class method and we have what we have the self as the argument.
So every time you create an object this init method has been called. So also this process is happening meaning register student will be called and the self which is the object which will be like for now student one will be provided as what as the element. So register student will be this one student. So student will be the class followed by what student another parameter but here you have password self the client object. So it student will be what? Student one the object that we have created at the bottom this one that was we were discussing about it. So you see now after you have provided those argument now again class it was what student not all is student.
for us which is this student of all is student now only list and we have seen that since a list was belonging to a class list and it was having methods and attribute we have seen one of the methods when we are learning the list was append meaning it we have seen it was adding something to the end of the list so you see class all the student the empty list that we have seen at the at the top not append meaning at Okay.
And and add at the end and the inside the method we are passing the argument as as what student my student was what student one the actual object. So the listing like student one like that. So the next line it was doing what class total student. So classes this student total student which is the attribute.
Now initially it was zero. So you are saying what class is equal to one meaning add one every time we append object add one meaning increment the value by one. So it call one again since this second line it is student two. So student two again another object. Okay. The init method construct will be called. It will initialize all of these attributes and then the the next the same process the next line student do register students the register student method will be called and this will be the self will be passed as what as the argument which will be the student which is the current object register student then the class will be student class student will be what student two so class dot all the students. So class all students list.
Okay. Now we have what? Student one.
It's no longer empty. So you see append the meaning to the last. So this student will become what? Student two. Remember follow along. So student replaced by what the current object which is student two. So here to the end. So comma student two. So this we are just visualizing but it's not just the actual code and but we I'm just showing you what's happening behind the scenes. So this is what happening behind the scenes. So again the next line is equal to one meaning the value. So two you see process.
So here we are just doing what we are calling we are calling the count student and method. So it will call the class which is the student student dot count student it will call the count student uh method then class student class which is a student we are just returning the total student. So student dot total student and we will get what two that's the reason why you are seeing that is the whole process I have explained it like that so let me go back and so that was just the behind the scenes I was just showing you so that is how it's happening so in a mania if we create an another object so let me create another object student three se and the password and then we say 22 then we say hey like that. So now again look what are you expecting will you expect two.
So every time the same process again it create here. So it go to the other line again. So we we can see what count studentma we can count how many object you have. So if you run the code you'll see what it has changed to three. So now this class method is working correctly.
Meaning it's showing us how many how many instances or object we have a class we have inside the class. Now let's call again also this class method list all student and we see how what is doing. So for us to access we can do what you can say print student not list all the students. It was this one list all student. So you will look at list all uh student. It's not having it's only having one parameter and this parameter you don't have to manually pass it. It will be called automatically. So class student. So you are just looping now you are using the full loop to iterate through the student and print each student name. So you saying for student class all student list that at the top what each time student variable it contain the current of the object. So student object dot name. So we'll see what student name. So let's run and we see the result.
Uh if we have printed the oh okay yes we are seeing we are seeing at the top we are seeing okay so let me let us use so that we don't see none use the return because at the bottom we have used what we have used the print so it will be a repetition so let me run again and then for us to see clearly let me this one at the bottom uh so that we see the result clearly. So list all uh students student dot list all students.
Okay.
Then we go back the list all students.
Okay, list students.
Okay, we are finally seeing everything.
Let's see how they have changed.
Let's burn it.
Okay.
And then just and remove this.
Okay, I wanted it to show it like that because it's how the print return it works. So, okay, I wanted it like to be like that. So you see every time we list now it's the list is student teacher class method it's showing us what every time it loops through the whole student list and it show what is student name.
So you see the first time the first object name which second time the second object the third time the third object student name samat like that.
So you see that how now that's how now a class uh can access method. So know that class uh this this method belongs to a class by using what the class method decorator.
So now you can differentiate that this and you know uh sometimes even the you can create a class with a class method and the instance or the object and still use it.
So uh the class methods can be accessed by what? By both the students class and also the object. But the object or the instance method can only be accessed by what? By object. Like even the same belongs uh this one also belongs the same to the to the uh class attribute. So the class attribute as you see like this all student all of the university or the total all can be accessed by the class and also the object like student one student two student three also the methods the methods may create mainly the main the lesson the main thing require to see how the classes can access but remember also instance can access but the methods like this which now it doesn't have the class method can only be accessed by what? By the object.
So that's how now the class methods works. So you use the class dot that method.
Uh that is what now the main thing that's uh we wanted to learn today.
相关推荐
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











