Python abstract base classes, difference between a mixin & abstract method

Question:

The table below displays various abstract base classes that are prevalent throughout Python. However, I am uncertain about their specific usage in this context.

Can you explain the distinction between the ‘Abstract Methods’ column and the ‘Mixin Methods’ column? Are the methods in one column optional to implement and those in the other column mandatory?

(Below Image) Official Python Reference – Abstract Base Classes for Containers


enter image description here

Asked By: AlanSTACK

||

Answers:

Anything in the Abstract Methods column, you have to implement yourself. The ABC provides default implementations of the methods in the Mixin Methods column, implemented in terms of the methods you have to write.

Answered By: user2357112

The abstract methods are those you have to define when you inherit from this ABC.

The Mixin column lists the methods you can use afterwards, you get them for free by inheriting not from object but from this ABC. If you define __getitem__, you can automatically use __iter__ afterwards for example.

Another benefit of inheriting from these classes is that anyone can see afterwards, what your class was meant to be, because it is issubclass(your_class, any_ABC) and any instance is isinstance(your_object, any_ABC). But this is written in the tutorial after the table you have posted.

Answered By: Ilja