DEV Community


Explain python classmethod and abstractmethod like I am five

mmphego profile image Mpho Mphego ãƒŧ1 min read

Care to explain classmethods and abc.abstractmethod in laymen terms... Like completely dumb it down.
A real life example would be nice

Discussion (4)

Editor guide
rhymes profile image
rhymes • Edited

A class method is exactly what the name says: you have a class and the class has a method.

The difference between instance methods and class methods in Python is that the first ones operate on an instance of the class. Class methods operate on the class itself.

The main purpose of methods is to operate on state. Instance methods operate on the state of a single object, class methods operate on the class itself (they don't have access to the object) and they are also available to all objects of that class.

I think you can totally ignore the abc module in Python in the beginning (though I think you can almost always ignore it :)) unless you're interested in building contracts between callers and implementors. They might be useful in big architectures. abc.abstractmethod basically means: "this method here is not a real implementation, I'm just saying to you that you need to override this method and provide an implementation when you use the base class I'm in".

mmphego profile image
Mpho Mphego Author

Now, this makes sense to me.
I've bookmarked this in case I need it again in future!

mmphego profile image
Mpho Mphego Author

Thanks for the explanation, but still confused hahahahaha