Home > Blockchain >  What is the type hint for a method?
What is the type hint for a method?

Time:01-20

What is the right type hint for a method? There is typing.Callable, but I'm looking for a method type hint, and typing.Callable[[Self, ...], ...] is not working.

I've tried this but it doesn't work:

class _Object:
    """An empty object for method type."""

    def method(self) -> None:
        """
        A method.

        :return None: Nothing.
        """
        return None


MethodType: Type = type(_Object().method)

MethodType is a type and not a type alias. Which type should I use then? I'm using Python 3.11.

CodePudding user response:

types.MethodType would be the annotation to use for "specifically a method object", but most of the time, typing.Callable will be more useful.

  • Related