title
Python OOP Tutorial 3: classmethods and staticmethods
description
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.
Python OOP 1 - Classes and Instances - https://youtu.be/ZDa-Z5JzLYM
Python OOP 2 - Class Variables - https://youtu.be/BJ-VvGyQxho
Python OOP 3 - Classmethods and Staticmethods - https://youtu.be/rq8cL2XMM5M
Python OOP 4 - Inheritance - https://youtu.be/RSl87lqOXDE
Python OOP 5 - Special (Magic/Dunder) Methods - https://youtu.be/3ohzBxoFHAY
Python OOP 6 - Property Decorators - https://youtu.be/jCzT9XFZ5bw
The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Object-Oriented
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/
#Python
detail
{'title': 'Python OOP Tutorial 3: classmethods and staticmethods', 'heatmap': [{'end': 57.338, 'start': 27.062, 'weight': 1}], 'summary': 'Tutorial explores python class methods, covering the difference between regular, class, and static methods, using class methods to automatically take the class as the first argument, altering method functionality, working with class variables, and using class methods as alternative constructors. it also discusses static methods in python classes and their comparison with class and regular methods, highlighting use cases and examples.', 'chapters': [{'end': 77.189, 'segs': [{'end': 61.46, 'src': 'heatmap', 'start': 27.062, 'weight': 0, 'content': [{'end': 29.744, 'text': 'And by convention, we were calling this self.', 'start': 27.062, 'duration': 2.682}, {'end': 34.747, 'text': 'So if a regular method automatically takes in the instance as the first argument,', 'start': 30.264, 'duration': 4.483}, {'end': 39.69, 'text': 'then how can we change this so that it instead automatically takes the class as the first argument?', 'start': 34.747, 'duration': 4.943}, {'end': 42.552, 'text': "Now, to do that, we're going to use class methods.", 'start': 40.17, 'duration': 2.382}, {'end': 51.336, 'text': "And to turn a regular method into a class method, it's as easy as adding a decorator to the top called class method.", 'start': 43.012, 'duration': 8.324}, {'end': 53.516, 'text': "So let's go ahead and create one of these.", 'start': 51.796, 'duration': 1.72}, {'end': 57.338, 'text': "So I'm gonna create a new method here with that class method decorator.", 'start': 53.897, 'duration': 3.441}, {'end': 61.46, 'text': "And I'm just gonna call this set raise amount.", 'start': 57.838, 'duration': 3.622}], 'summary': 'Using class methods to automatically take the class as the first argument.', 'duration': 61.211, 'max_score': 27.062, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M27062.jpg'}], 'start': 0.249, 'title': 'Python methods', 'summary': 'Explains the difference between regular, class, and static methods in python, addressing the confusion between class methods and static methods, and demonstrates the use of class methods to automatically take the class as the first argument, with an example of the set raise amount method.', 'chapters': [{'end': 77.189, 'start': 0.249, 'title': 'Python methods: regular, class, and static', 'summary': 'Explains the difference between regular, class, and static methods in python, addressing the confusion between class methods and static methods, and demonstrates the use of class methods to automatically take the class as the first argument, with an example of the set raise amount method.', 'duration': 76.94, 'highlights': ['The chapter explains the difference between regular, class, and static methods in Python.', 'Demonstrates the use of class methods to automatically take the class as the first argument, with an example of the set raise amount method.', 'Regular methods in a class automatically take the instance as the first argument, conventionally called self.']}], 'duration': 76.94, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M249.jpg', 'highlights': ['The chapter explains the difference between regular, class, and static methods in Python.', 'Demonstrates the use of class methods to automatically take the class as the first argument, with an example of the set raise amount method.', 'Regular methods in a class automatically take the instance as the first argument, conventionally called self.']}, {'end': 264.671, 'segs': [{'end': 171.682, 'src': 'embed', 'start': 77.59, 'weight': 0, 'content': [{'end': 85.678, 'text': 'But basically, this is altering the functionality of our method to where now we receive the class as our first argument instead of the instance.', 'start': 77.59, 'duration': 8.088}, {'end': 87.98, 'text': 'Now by convention with a regular method.', 'start': 86.058, 'duration': 1.922}, {'end': 97.184, 'text': "We called this instance variable self, and there's a common convention for class variables too, and that is cls.", 'start': 88.42, 'duration': 8.764}, {'end': 104.527, 'text': "Now we can't use the word class as the variable name here because the word has a different meaning within the language.", 'start': 97.584, 'duration': 6.943}, {'end': 110.271, 'text': 'You can see here up at the top that we use the word class to create a new class.', 'start': 104.867, 'duration': 5.404}, {'end': 112.113, 'text': 'So that is a keyword in Python.', 'start': 110.411, 'duration': 1.702}, {'end': 115.215, 'text': "So instead we're going to use CLS as our class variable name.", 'start': 112.413, 'duration': 2.802}, {'end': 122.04, 'text': 'So now within this set raise amount method, we are working with the class instead of the instance.', 'start': 115.655, 'duration': 6.385}, {'end': 127.544, 'text': "And to show you what I mean by this, let's go ahead and set our class variable raise amount.", 'start': 122.401, 'duration': 5.143}, {'end': 138.726, 'text': "So we'll say class.raiseAmount And we'll just set this equal to the amount argument that we are accepting from this method.", 'start': 128.305, 'duration': 10.421}, {'end': 144.948, 'text': 'So now down here at the bottom, I still have these two employee instances that I created in our previous tutorials.', 'start': 139.126, 'duration': 5.822}, {'end': 151.95, 'text': 'And here I am printing out the classes raise amount as well as both instances raise amounts.', 'start': 145.408, 'duration': 6.542}, {'end': 157.972, 'text': 'So if I go ahead and run this, then you can see that all of those are equal to 4%.', 'start': 152.27, 'duration': 5.702}, {'end': 165.617, 'text': 'Now the reason all those are equal to 4%, again, is because we have this class variable here, raise amount, that is set to 4%.', 'start': 157.972, 'duration': 7.645}, {'end': 169.48, 'text': "So now, let's say that we wanted to change this to 5%.", 'start': 165.617, 'duration': 3.863}, {'end': 171.682, 'text': 'So before I print these out,', 'start': 169.48, 'duration': 2.202}], 'summary': 'Method functionality altered to receive class as first argument, setting class variable raise amount to 4%, demonstrating impact on instances.', 'duration': 94.092, 'max_score': 77.59, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M77590.jpg'}, {'end': 264.671, 'src': 'embed', 'start': 216.512, 'weight': 1, 'content': [{'end': 230.358, 'text': 'us running this set raise amount method here and setting this CLS variable raise amount to the amount is the same thing as us saying employee.raiseAmount equals 5%,', 'start': 216.512, 'duration': 13.846}, {'end': 234.1, 'text': 'but now we are using this class method to do that instead.', 'start': 230.358, 'duration': 3.742}, {'end': 241.629, 'text': "Now you can run class methods from instances as well, but that doesn't really make a lot of sense and I don't ever see people doing it.", 'start': 234.68, 'duration': 6.949}, {'end': 250.92, 'text': 'But to show you what that would look like, I can also grab this instance here and I can run that class method from the instance.', 'start': 241.949, 'duration': 8.971}, {'end': 252.721, 'text': 'And if I run that,', 'start': 251.4, 'duration': 1.321}, {'end': 264.671, 'text': 'you can see that running that class method from the instance still changes that class variable and sets all of the class variable and both instance amounts to that 5% that we passed in.', 'start': 252.721, 'duration': 11.95}], 'summary': 'Using class method to set raise amount to 5%.', 'duration': 48.159, 'max_score': 216.512, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M216512.jpg'}], 'start': 77.59, 'title': 'Python class method basics and class variable', 'summary': "Covers altering method functionality to receive the class as the first argument, using 'cls' for class variables, and working with the class within a method. it also explains class variables and methods, demonstrating changing the class variable raise amount from 4% to 5%, affecting all instances.", 'chapters': [{'end': 122.04, 'start': 77.59, 'title': 'Python class method basics', 'summary': "Explains altering the functionality of a method to receive the class as the first argument, using 'cls' as the convention for class variables, and working with the class within a method.", 'duration': 44.45, 'highlights': ["Using 'cls' as the convention for class variables instead of 'class' due to the latter being a keyword in Python.", 'Altering the functionality of a method to receive the class as the first argument.', 'Working with the class within a method.']}, {'end': 264.671, 'start': 122.401, 'title': 'Class variable and class method', 'summary': 'Explains the concept of class variables and class methods, demonstrating how to set and change the class variable raise amount, resulting in all instances reflecting the updated raise amount, such as changing it from 4% to 5%.', 'duration': 142.27, 'highlights': ['The concept of class variables and class methods is explained, with a demonstration of setting the class variable raise amount and reflecting the changes in all instances.', 'The process of changing the class variable raise amount from 4% to 5% is demonstrated, showcasing the impact on all instances.', 'The functionality of the set raise amount method, as a class method, is explained in detail, emphasizing its role in updating the class variable raise amount for all instances.', 'The example of running a class method from an instance is provided, highlighting its impact on the class variable and instance raise amounts.']}], 'duration': 187.081, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M77590.jpg', 'highlights': ['The process of changing the class variable raise amount from 4% to 5% is demonstrated, showcasing the impact on all instances.', 'The example of running a class method from an instance is provided, highlighting its impact on the class variable and instance raise amounts.', 'The functionality of the set raise amount method, as a class method, is explained in detail, emphasizing its role in updating the class variable raise amount for all instances.', "Using 'cls' as the convention for class variables instead of 'class' due to the latter being a keyword in Python.", 'Altering the functionality of a method to receive the class as the first argument.', 'Working with the class within a method.', 'The concept of class variables and class methods is explained, with a demonstration of setting the class variable raise amount and reflecting the changes in all instances.']}, {'end': 909.416, 'segs': [{'end': 299.65, 'src': 'embed', 'start': 266.112, 'weight': 0, 'content': [{'end': 271.013, 'text': 'Now, you may also hear people say that they use class methods as alternative constructors.', 'start': 266.112, 'duration': 4.901}, {'end': 280.156, 'text': 'Now, what do they mean by this? So what they mean is that you can use these class methods in order to provide multiple ways of creating our objects.', 'start': 271.433, 'duration': 8.723}, {'end': 286.338, 'text': "So let's say that, for example, we had someone who was using our employee class And they said hey,", 'start': 280.736, 'duration': 5.602}, {'end': 299.65, 'text': "I have these specific use cases where I'm getting employee information in the form of a string that is separated by hyphens and I'm constantly needing to parse the string before I create new employees.", 'start': 286.338, 'duration': 13.312}], 'summary': 'Class methods can provide multiple ways of creating objects, such as parsing strings to create new employees.', 'duration': 33.538, 'max_score': 266.112, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M266112.jpg'}, {'end': 408.387, 'src': 'embed', 'start': 380.864, 'weight': 1, 'content': [{'end': 385.425, 'text': "then we don't want them to have to parse these strings every time that they want to create a new employee.", 'start': 380.864, 'duration': 4.561}, {'end': 393.207, 'text': "So let's just create an alternative constructor that allows them to pass in the string and we can create the employee for them.", 'start': 385.825, 'duration': 7.382}, {'end': 398.448, 'text': "So I'm going to go ahead and come up here and create a new class method.", 'start': 393.687, 'duration': 4.761}, {'end': 403.484, 'text': "And we're going to use this new method as an alternative constructor.", 'start': 400.282, 'duration': 3.202}, {'end': 408.387, 'text': "And usually these start with the from, but that's just a convention also.", 'start': 404.085, 'duration': 4.302}], 'summary': 'Creating an alternative constructor for employee class method to pass in a string and create an employee.', 'duration': 27.523, 'max_score': 380.864, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M380864.jpg'}, {'end': 586.301, 'src': 'embed', 'start': 555.871, 'weight': 2, 'content': [{'end': 559.012, 'text': 'And there are several ways that we can create new date times.', 'start': 555.871, 'duration': 3.141}, {'end': 564.714, 'text': 'And if you search for class method within the date time module, then you can see an example of some of these.', 'start': 559.052, 'duration': 5.662}, {'end': 573.737, 'text': 'So the default way of creating a date time object is to say something like date time and then pass in the year, month and date.', 'start': 565.214, 'duration': 8.523}, {'end': 578.538, 'text': 'But if we look here at these class methods, which are alternative constructors,', 'start': 574.157, 'duration': 4.381}, {'end': 586.301, 'text': 'then what they do is they have this from timestamp and you can use the current time, which is today.', 'start': 578.538, 'duration': 7.763}], 'summary': 'Ways to create new date times using class methods in the date time module.', 'duration': 30.43, 'max_score': 555.871, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M555871.jpg'}, {'end': 645.877, 'src': 'embed', 'start': 619.145, 'weight': 4, 'content': [{'end': 623.268, 'text': "Okay, so now that we've looked at class methods, now let's talk about static methods.", 'start': 619.145, 'duration': 4.123}, {'end': 626.891, 'text': 'Now, a lot of people get class methods and static methods confused.', 'start': 623.648, 'duration': 3.243}, {'end': 634.956, 'text': 'Now, when working with classes, regular methods automatically pass the instance as the first argument, and we call that self.', 'start': 627.331, 'duration': 7.625}, {'end': 642.376, 'text': 'And class methods automatically pass the class as the first argument, and we called that CLS.', 'start': 636.034, 'duration': 6.342}, {'end': 645.877, 'text': "And static methods don't pass anything automatically.", 'start': 642.976, 'duration': 2.901}], 'summary': 'Introduction to static methods in python classes.', 'duration': 26.732, 'max_score': 619.145, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M619145.jpg'}, {'end': 720.767, 'src': 'embed', 'start': 692.557, 'weight': 3, 'content': [{'end': 694.639, 'text': "And we're also going to use a decorator.", 'start': 692.557, 'duration': 2.082}, {'end': 697.061, 'text': 'That decorator is going to be static method.', 'start': 694.999, 'duration': 2.062}, {'end': 701.164, 'text': "So I'm going to call this method is workday.", 'start': 697.481, 'duration': 3.683}, {'end': 707.324, 'text': "Now remember, static methods don't take the instance or the class as the first argument.", 'start': 702.403, 'duration': 4.921}, {'end': 710.305, 'text': 'So we can just pass in the arguments that we want to work with.', 'start': 707.364, 'duration': 2.941}, {'end': 716.606, 'text': "And I'm going to take in a day here and return whether or not that is a workday.", 'start': 710.665, 'duration': 5.941}, {'end': 720.767, 'text': "So to make this a simple example, I'm not going to do anything too complicated.", 'start': 717.046, 'duration': 3.721}], 'summary': 'Using a static method decorator to determine workdays without complex operations.', 'duration': 28.21, 'max_score': 692.557, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M692557.jpg'}], 'start': 266.112, 'title': 'Using class and static methods in python', 'summary': 'Discusses using class methods as alternative constructors to create objects, illustrated with an example of parsing employee information and comparison to datetime module. it also explains static methods in python classes, comparing them with class and regular methods, and demonstrates the implementation of a static method for checking workdays, providing examples and usage of decorators. the difference between regular instance methods, class methods, and static methods is highlighted with their respective use cases and examples.', 'chapters': [{'end': 618.605, 'start': 266.112, 'title': 'Using class methods as alternative constructors', 'summary': "Discusses using class methods as alternative constructors to provide multiple ways of creating objects, with an example of parsing employee information from a string and a comparison to the datetime module's alternative constructors.", 'duration': 352.493, 'highlights': ['Class methods can be used as alternative constructors to provide multiple ways of creating objects.', 'Example of parsing employee information from a string using an alternative constructor.', "Comparison to the datetime module's alternative constructors for creating datetime objects."]}, {'end': 909.416, 'start': 619.145, 'title': 'Static methods in python', 'summary': 'Explains the concept of static methods in python classes, comparing them with class methods and regular methods, and demonstrates the implementation of a static method for checking workdays, providing examples and usage of decorators. the chapter also discusses the difference between regular instance methods, class methods, and static methods, highlighting their use cases and providing an example for each.', 'duration': 290.271, 'highlights': ['The chapter explains the concept of static methods in Python classes, comparing them with class methods and regular methods, and demonstrates the implementation of a static method for checking workdays, providing examples and usage of decorators.', 'The chapter also discusses the difference between regular instance methods, class methods, and static methods, highlighting their use cases and providing an example for each.']}], 'duration': 643.304, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rq8cL2XMM5M/pics/rq8cL2XMM5M266112.jpg', 'highlights': ['Class methods can be used as alternative constructors to provide multiple ways of creating objects.', 'Example of parsing employee information from a string using an alternative constructor.', "Comparison to the datetime module's alternative constructors for creating datetime objects.", 'The chapter explains the concept of static methods in Python classes, comparing them with class methods and regular methods, and demonstrates the implementation of a static method for checking workdays, providing examples and usage of decorators.', 'The chapter also discusses the difference between regular instance methods, class methods, and static methods, highlighting their use cases and providing an example for each.']}], 'highlights': ['Class methods can be used as alternative constructors to provide multiple ways of creating objects.', 'The process of changing the class variable raise amount from 4% to 5% is demonstrated, showcasing the impact on all instances.', 'The chapter explains the difference between regular, class, and static methods in Python.', 'The chapter explains the concept of static methods in Python classes, comparing them with class methods and regular methods, and demonstrates the implementation of a static method for checking workdays, providing examples and usage of decorators.', 'Demonstrates the use of class methods to automatically take the class as the first argument, with an example of the set raise amount method.', 'Regular methods in a class automatically take the instance as the first argument, conventionally called self.', 'Example of parsing employee information from a string using an alternative constructor.', 'The functionality of the set raise amount method, as a class method, is explained in detail, emphasizing its role in updating the class variable raise amount for all instances.', "Comparison to the datetime module's alternative constructors for creating datetime objects.", "Using 'cls' as the convention for class variables instead of 'class' due to the latter being a keyword in Python."]}