#25 - (OOP) Instance method, Class method & Static method in Python
HTML-код
- Опубликовано: 21 сен 2024
- Learn about instance method, class method and static method in python class. Instance method is the most common method in python class and is also called as regular method. Instance method takes object i.e. 'self' as it's first parameter. Inside an instance method, we can access class & instance variables with self attribute.
Class method takes the class i.e. 'cls' as it's first parameter. We can use different name as well, but the convention is to use 'cls' to define a class parameter. Inside a class method, we can access class variables with class attribute. We have to use @classmethod decorator to define a class method.
Static method does not need to have an object or class as it's first parameter. We only include them in class because they have a logical connection. For the static method, there should be no use of class or instance variable.
Facebook: / techvitals
I'm Brazilian and I'm learning python, and even with the language difficulty I managed to learn from your video what I didn't learn from the Brazilian videos! Thanks.
This means a lot to me. Thanks for the acknowledgment.
@@TechVitals :)
Such a short video...But the concept was so clearly explained...On the other hand there are some really long ones with no clear message... Thank you.
Keep it up, dude.
Nice video
Very well explained in a simple and clean way, thank you!
Good explanation....
Thank you for the support.
Thanks for the video!! It really helped me understand these concepts better
My pleasure!!
Thanks for the video. Can you please let me know why do we write self.first_name = first_name? What is its significance?
first_name is a parameter that holds the value sent through the object, and self.first_name is the instance variable that actually stores the value for the object. Therefore, when we create an object for a class, we send the value using the first_name parameter and then inside __init__(...), we save the value for that object using self.first_name. NOTE: self refers to the object of the class, so self.first_name means the first_name of that object for a class. Hope this helps.
This is explain very well, thank you!
Appreciated :)
Nice video Thank you for the explanation!
Pleasure brother!
Thanks for this video!
nice tutorial, thanks... got thinking, what if instead of creating a class for change_company(new_name), you add "new_name" as a parameter in the instance method? so it would be Employee(self, first_name, last_name, company).. I guess it would be possible to do so? and how the class method is better than that? thanks!
yes, its possible. But use case for class attribute "company" is when a company wants to keep record of all of its employees. So in that case we need not pass company every time we create an employee record, as it remains same. We would use class method change_company() only when company gets a new name. Hope this helps
keep it up...
Thank you buddy.
nice