【问题标题】:What is the type hint for a method?方法的类型提示是什么?
【发布时间】:2023-01-22 12:15:40
【问题描述】:

method 的正确类型提示是什么?有typing.Callable,但我正在寻找方法输入提示,typing.Callable[[Self, ...], ...] 不工作。

我试过这个但它不起作用:

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

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

        :return None: Nothing.
        """
        return None


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

方法类型是类型而不是类型别名.那我应该使用哪种类型?我正在使用 Python 3.11。

【问题讨论】:

    标签: python methods python-typing


    【解决方案1】:

    types.MethodType 将是用于“特定方法对象”的注释,但大多数时候,typing.Callable 会更有用。

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多