In this video, you’ll see an example of both static methods and class methods used in a Python class. You’ll also see how these methods work when dealing with sub classes.
How @staticmethod and @classmethod Work With Subclasses
Next, let’s look to see how static methods and class methods work when dealing with subclasses. So, here we have a generic class called
Animal, which takes a
color and has a few methods defined. One class method, which takes a
cls (class) argument as the first argument, and randomly picks a color for the child and returns a new instance of that particular class.
Then we have a couple of subclasses of that particular type. One that overrides the
.speak() method, so when an animal speaks—when a dog speaks, in this case—
'Bark!' is printed rather than
'Roar!' And we also have a way of instantiating and calling the class for a dog.
00:52 So, we have this particular thing commented out. I have that line commented out just to show you that when we try to run this without telling it to run on the superclass, it causes a bit of a problem.
So, let’s just run through this and see. This will run fine, no big deal. We have an example here that says we created a
Dog, which takes the color
'Brown'. We print that
.color. So as you can see here, we print the
.color of the dog, which is
When we try to access the
.color attribute on the puppy, we get no attribute. It just doesn’t exist. You don’t have it, you don’t exist. Which is interesting, because when this returns, it returns a
01:58 So all this says, it goes, “This, the class I’m in—make a baby.” So we can call up to the class method. Now, this is an explicit way of doing the same thing here, so if we were to just leave this all uncommented—so let’s just delete it here.
Just one second, let’s get that into the buffer here so we can easily get that back afterwards. Let’s run that and then see what happens. So, as you can see, this works completely fine. We call
d.make_baby(), and then we have
pup here and then we check to see the
So this time, we returned a
Dog which is
Golden, which is quite awesome. Now let’s put back what we had here before and see if we can grok what that does. So, when you run it this time, it won’t print the class. No, sorry. It will print the class, but it will also print
'making dog baby' just before.
making dog baby and then it went through and ran through all of the static method stuff required up in here. Now, this is an idiom that you may see in
.__init__() methods, where you want to do some special behavior and then call your superclass to finish doing whatever you’re doing. The same can be done with class methods, so let’s say we wanted to let somebody know that something happened.
We’d do that information here. So,
# do something dog specific. Then we would then call the superclass to finish up the remainder of the work, which involves making a baby. So, you can either choose to do this, or you can simply delegate this to your superclass like we did here, or you can just leave this removed, like we had in the previous example, and simply rely on the superclass’s class method to invoke your exact behavior.
But the key thing to remember here is this, that this class method is based on either the instance or the class in which you run the method on. So in this example,
d. Or in the previous example of
Next, let’s take a look at the static methods. Now, the static method for the
Animal—it only simply returns and prints
'Roar!' Now, for the
Dog method, dogs clearly don’t roar, we want them to bark, so we’d have to override this behavior.
Sorry, I’ve got to reevaluate that and I’ve got to reevaluate this. Now, in all cases here that this
.speak() will print a
'Bark!', the instance of
.speak() will print
'Bark!', and the class running
.speak() will print
'Bark!' This just goes to show that you can override these methods in your subclasses and they will do what the specific thing will do. So, for example, if we were to go here
Animal.speak(), as you might tell, this will print
'Roar!' Now let’s take a look at the class example of
Cat we did nothing, all we did was subclass the
Animal class in
So when you do anything running on
Cat, it will simply produce—again, here for the class method, it took the class which the method was called on, which is
c, which is of type
Cat. So, as you can see here, it returns how to make a cat, prints
'Black' and returns
'Roar!', as we showed here, when we weren’t overriding it.
Become a Member to join the conversation.