Python OOP Tutorial 3: classmethods and staticmethods

preview_player
Показать описание
In this Python Object-Oriented Tutorial, we will be learning about classmethods and staticmethods. Class methods are methods that automatically take the class as the first argument. Class methods can also be used as alternative constructors. Static methods do not take the instance or the class as the first argument. They behave just like normal functions, yet they should have some logical connection to our class. We will look at some examples of both of these in order to understand both in depth. Let's get started.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

Summary:
In this video, Corey distinguishes between a regular method, class method, and a static method.


Firstly, a regular method is the type of method that we are used to seeing since the start of OOP tutorials. It is accessible through both the class and the instance, which means that we can call for the method in both
Employee.method()
and
emp_1.method()
they automatically have the instance as the first positional argument, as self.




First is modifying the class variable. Corey modified the 'raise_amount' class variable using a class method. Just remember that to access a class variable, we have to write 'cls.' before specifying the actual name. For example, as 'cls.raise_amount' as in the video.


Second is making an alternative constructor. Sometimes people have information of their specific instances of the class available in a specific format. Corey shows an example of this where first and last names and pay are separated by a hyphen. Corey creates a class method that returns the class with the specific values passed in that are obtained by using split() method to the string passed in. User of the script can now automatically create a new instance without having to parse the string at '-'.


팍준하
Автор

I paused at 1:17 to checkout video on Decorators which I paused again to checkout video on Closures which I paused, yet again to checkout video on First Class Functions. Glad, I did. Thanks for the awesome videos.

kushkaul
Автор

He actually knows what to teach,
Unlike most of paid courses.

nishantnavade
Автор

At least have some ads. You are too good to handle. I'm feeling guilty for watching these videos for free.

ankitdhungana
Автор

I'm broke right now but your tutorials are helping pave the way for me to get out of this situation. Once I get back on my feet I'm going to send some loot to your patreon. Thank you for teaching. These are awesome tutorials.

nackyding
Автор

Easily the best explanations of Python concepts on any platform ( YouTube, Udemy, Code with Mosh etc.) Corey has a great ability when explaining something to stay within the scope of the topic. By that i mean he doesn't mentioned anything that would be unfamiliar to someone at that particular level. I've found tutors will throw in advanced topics at a very early stage and throw you complete off. Another helpful aspect of Coreys tutorials is his naming of variables/methods/classes and concepts within code. I've found that tutors will name these similar to the concept they are explaining which has lead me completely misunderstand a subject. I really appreciate your tutorials.

richardhowlin
Автор

When I was a beginner, I have watched this video and I learned a couple of things. Now that I have intermediate skills, I see the depth and beauty of this video clearly. I feel like I truly grasped this concept. Thanks Corey!

AydinCGur
Автор

I cann't imagine you make this tutorial free for public. This is the best videos over the internet I think. Thank you man. You are great.

Land-management-system-bd
Автор

It has been 5 years, since you uploaded this video, and I bet their is still no video or teacher that explain me this topic better than you have. You are the best!!!

gamerforever
Автор

dude, this is like the best tutorial out there. I've been trying to understand class methods, and I've just been confused at how other people have taught it, but Corey Schafer's beginner friendly way of teaching and his way of just explaining all the little things just helped me clear out a lot of the rubble for me.

cubeow
Автор

That was beautiful. Such simple and succinct explanation. I have wasted almost 2 hours trying to understand this by reading and nothing I read helped me as much as this video.

akshaybhardwaj
Автор

Hey Corey, You are awesome. I see the hard work you have put through these videos and sharing the fruits of your effort with us.

sudarsandm
Автор

you have a very firm and clear voice....it pulls my concentration : )

prathamva
Автор

I am completely stuck on this specific topic for a Python course I am taking. This is the third video I've watched and I must say that your ability to explain these concepts is awesome. Thank you sir!

juliusarceo
Автор

I tried to learn the class concept several times from tutorial videos.
This is the first time I really feel I got it right.
Thanks.

jack_s
Автор

Your videos make more sense and are easier to understand than anybody else!

artmcclure
Автор

Hi Corey,

I noticed a small mistake at 5:46.
When you split the string, you create new strings including the pay variable.
If you were to execute the apply_raise() function on that new_emp_1 variable, the function would try to multiply a float by a string, getting an error.
We need to convert that string to an integer
So that line 34. should read: new_emp_1 = Employee(first, last, int(pay))
Same goes for the subsequent from_string classmethod created using the same way.

Great videos by the way, I am learning tons!

pyb.
Автор

Explained in much more concise detail than several of the paid programming course websites out there. Keep up the good work

thatonenerd
Автор

Man, I never understood classes and their use until I came across your videos.. They are so easy to understand and you are like the best teacher Thanks for all your efforts and thank you for these videos.

chiky
Автор

OOP is no more remained difficult after watching these. No doubt, lot of hard-work is done for making these master piece tutorials and hard-work always pays.

umerchohan
join shbcf.ru