Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What Makes Static Methods Different?

Give Feedback

This lesson covers the differences between ordinary functions and @staticmethods. You’ll see a real world example of how to use @staticmethod and also learn when you’d want to use them.

00:00 You may now be wondering how are static methods and class methods any different from any other function in Python. First, let’s talk about static methods.

00:10 Static methods can be considered simple functions that take arguments and compute something and return the result. It has no information about the class or instance it’s called upon. For example, as you can see here, we have a static method defined in the class Date, which takes a datetime.

00:27 The method here simply checks to see if a particular datetime is naive or not, by simply checking the .tzinfo. If the .tzinfo is None, that means the datetime is naive and we return True, else we return False.

00:42 This is not any different from having a module-level function that does the exact same thing. Next, we define a datetime object which is naive and a datetime object which is localized.

00:58 As you can see below, d1 is a localized datetime and d2 is a naive datetime. Next, we simply create an instance of the Date object and we assign that to d. We are then able to call the .is_naive() method against that particular datetime. As you can see below, it produces False for d1 and True for d2.

01:24 This is not any different from simply calling is_naive(), which we defined above outside of the function, and passing d1

01:34 and passing d2.

01:42 These are all functionally equivalent. Sorry, this masked the first one, so just give me a second here. As you can see, these are all functionally equivalent methods.

01:55 Now, static methods are just special functions. They serve no other purpose other than for organizational purposes. For example, this .is_naive() method really fits tightly and closely knit with the Date object.

02:12 Now, it may be useful outside, but we can still simply just use the class to call the .is_naive().

02:18 Most of the time, you will never need to use static methods because you can simply define a method outside at the module level, which is easily importable.

02:28 But when doing things that are needed to be repeated, you can simply bind it with the class that you’re using and then call it like we just did down here.

Become a Member to join the conversation.