Hi, Neil.

> I want to use abstractmethod, but I have my own metaclasses and I don't want
> to build composite metaclasses using abc.ABCMeta.
>
> Thanks to PEP 487, one approach is to facator out the abstractmethod checks
> from ABCMeta into a regular (non-meta) class.  So, my first suggestion is to
> split abc.ABC into two pieces, a parent regular class with metaclass "type":
>

I'm +1 with your idea in performance point of view.

Some people having other language background (C# or Java)
want to use ABC like Java's interface.

But ABC is too heavy to use only for checking abstract methods.
It uses three inefficient WeakSet [1] and it overrides isinstance and issubclass
with slow Python implementation.

[1] WeakSet is implemented in Python, having one __dict__, list and two sets.

And C implementation of gathering abstract methods will reduce
Python startup time too. Because `_collections_abc` module has many ABCs and
`os` module import it.

## in abc.py

# Helper function.  ABCMeta use this too.
# And Python 3.7 can have C implementation of this.

def _init_abstractclass(cls, bases=None):
    # Compute set of abstract method names
    if bases is None:
        bases = cls.__bases__
    abstracts = {name
                 for name, value in vars(cls).items()
                 if getattr(value, "__isabstractmethod__", False)}
    for base in bases:
        for name in getattr(base, "__abstractmethods__", set()):
            value = getattr(cls, name, None)
            if getattr(value, "__isabstractmethod__", False):
                abstracts.add(name)
    cls.__abstractmethods__ = frozenset(abstracts)


class Abstract:
    __init_subclass__ = _init_abstractclass


## usage

import abc

class AbstractBar(abc.Abstract):
    @abc.abstractmethod
    def bar(self): ...


Bests,

INADA Naoki  <songofaca...@gmail.com>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to