In this lesson you’ll dive deeper into the concepts behind instance, class and static methods. The differences are examined using a sample class.
Regular Instance Methods vs Class Methods vs Static Methods
So, I created this class, I call it
MyClass. It’s just a really simple example, and it’s got a couple of different method types on it. So, the first one is just called
.method(). The method is called
.method(), and it’s just a plain instance method. And you can see here, it takes one argument, the
self argument, which points to the object instance.
That means within
.method(), we can actually modify or read attributes on the object instance. And when you compare that to a class method—so, I just called this one
.classmethod() and then use the
@classmethod decorator to actually mark it as a class method—you can see here that the
.classmethod() only has access to this
cls (class) parameter, or
cls argument. It doesn’t have have a
So, this means a class method it can only access the class itself—or the object representing the class, because well, everything is an object in Python. But the key difference is that when I create an instance of
MyClass and call
.classmethod() on it, it won’t be able to actually access the
self object, right?
You just define a regular method and then mark it with the
@staticmethod decorator. What you can see here is that it doesn’t take any arguments at all, so it has no access to the class or the object instance at all.
01:43 It’s completely separate from that, and it’s really just a way to namespace your methods. So now, you know, I know this is very theoretical at this point, and it’s going to become much more clear when we actually try and do some experimentation with this stuff—some hands-on work.
Become a Member to join the conversation.