bpo-32380: Create functools.singledispatchmethod by emmatyping · Pull Request #6306 · python/cpython (original) (raw)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having an example stacked with @classmethod would be good here. It's unfortunate that the order of application will need to be different from that when stacking @abstractmethod (see https://docs.python.org/3/library/abc.html#abc.abstractmethod), but we need "register" to be accessible on the resulting descriptor. So something like:

class Negator:
    @singledispatchmethod
    @classmethod
    def neg(cls, arg):
        ...

Given the @classmethod example, you can then note that the same principle applies for @staticmethod and other decorators - to be useful, @singledispatchmethod typically needs to be the outermost decorator so that the register method is accessible.