DEV Community

Mpho Mphego
Mpho Mphego

Posted on

Explain python classmethod and abstractmethod like I am five

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

Latest comments (3)

Collapse
 
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".

Collapse
 
mmphego profile image
Mpho Mphego

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

Collapse
 
mmphego profile image
Mpho Mphego

Thanks for the explanation, but still confused hahahahaha