[issue32060] Should an ABC fail if no abstract methods are defined?

Raymond Hettinger report at bugs.python.org
Fri Nov 17 14:45:27 EST 2017


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

FWIW, I should have add a bit more explanation.  The logic for ABCs instantiation is roughly:

    if any(method.is_abstract() for method in dir(cls)):
        raise TypeError('I refuse to instantiate')
    inst = instantiate(cls)

The edge case for any() is to default to False for an empty input sequence and this logic applies to ABCs as well.

Alternatively, you can think of the it as using all() style logic, "instantiate this class only if all of the methods are non-abstract even if the class has no methods at all".  The edge case for all() is that it returns True for an empty input.

Another way of describing the logic is that the normal case for instantiating a class is to create an instance unless there is still work to be done (as evidenced by the presence of one or more abstract methods).  

The abstract methods are essentially a todo list for subclassers.  If there are no open todos (even if there were never any todos), instantiation can go forward -- "You can't build that building until you have all the permits, but there are no permit requirements in this case, so go ahead with the construction."

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32060>
_______________________________________


More information about the Python-bugs-list mailing list