k-sok (3) [Avatar] Offline
#1
Hello!

In chapter 20.7, the only mentioned use case for language-enforced abstract bases classes is for a function to determine the type of the arguments. Taking into account other statements throughout the book, one gets the impression that in any other case everything should be "modeled" by the means of duck typing.

Don't get me wrong, it's not indented to be a philosophical discussion a la "dynamic typing vs. static typing" etc. But given the statement, that an interface (abstract base class) in python serves only for explicit type checking, I get the impression that a fundamental aspect of interfaces and OOP modeling in general is not addressed at all.

For me, independently of static and dynamic typing, an interface is a programmer's obvious statement clearly identifying an extension point in the system. Such kind of information is invaluable for people who are trying to adapt, extend and add new features to the system. Furthermore, how else one should be abel to identify related classes? Using duck typing one would have to compare available method signatures etc. (what would still give no hint about the intended semantics) or rely on comments.

After about 14 years of strictly typed languages, I'm now in a Python project, where I'm about to perform a refactoring and I'm faced with the fact that I've no idea about what is the best way to do it in Python. In a strict typed language I would define an interface (or ar a base class with only some abstract methods) and a couple of derived classes. In Python I have several possibilities:

1) ABCMeta with @abstractmethod (seems to be the wrong choice after chapter 20.7)
2) A base class with "abstract" methods throwing NotImplementedError containing assert False
3) No base class at all, just a couple of classes with the same set of methods (in case there is no common code)

What would be the pythonic way to do it?

Thank you in advance
Konstantin
naomi.ceder (134) [Avatar] Offline
#2
Re: ABCMeta meant only for explicit type checking?
Hi,

I didn't mean to give the impress that the only use case for ABC's was explicit type checking. However, it is perhaps the most easy to explain. There are other use cases, but most of them are highly specialized. The standard wisdom is that if you have a strong case, for things like ABC's you should use them and be happy, but if you don't already know what you might use them for, don't even try to to use them.

As to your project, the answer is, of course, "it depends" smilie Choice 1 might well be the Pythonic way of doing it, but do be careful not to just write Java or C++ code using Python. Choice 2 strikes me as the least Pythonic, although I can imagine cases where it might be the best solution, I'm suspecting that choice 3 has already been rejected by you as too simple minded...

So it really depends upon the situation - if you think that ABC's will make your code simpler, more elegant and easier to maintain, then that would be the Pythonic solution.

Best regards and thanks for writing!