[Python-ideas] Fwd: Consider allowing the use of abstractmethod without metaclasses

INADA Naoki songofacandy at gmail.com
Thu Jul 20 21:03:56 EDT 2017

> I wonder if it would make sense to go further and merge *both* of these
> features into regular classes.
> Checking for @abstractmethod in type.__new__ surely can't be that expensive,
> can it?

But it affects startup time.
It iterate all of the namespace and tries `getattr(obj,
`__isabstractmethod__`, False).
It means massive AttributeErrors are raised and cleared while loading
large library.

OTOH, I have another idea:

class AbstractFoo:
    def foo(self):
    __abstractmethods__ = ("foo",)

In this idea, `type.__new__` can check only `__abstractmethods__`.

> And if regular types supported 'register', then it would allow for a
> potentially simpler and faster implementation. Right now,
> superclass.register(subclass) has to work by mutating superclass, because
> that's the special ABCMeta object, and this leads to complicated stuff with
> weakrefs and all that. But if this kind of nominal inheritance was a basic
> feature of 'type' itself, then it could work by doing something like
>   subclass.__nominal_bases__ += (superclass,)
> and then precalculating the "nominal mro" just like it already precalculates
> the mro, so issubclass/isinstance would remain fast.

I don't like it.
In 99.9% of my classes, I don't need register based subclassing.

> I guess enabling this across the board might cause problems for C classes
> whose users currently use isinstance to get information about the internal
> memory layout.
> -n

More information about the Python-ideas mailing list