简体   繁体   English

是否有标准方法来指定docstring中的回调函数的类型?

[英]Is there a standard way to specify the type of a callback function in a docstring?

I have a function which accepts a callback. 我有一个接受回调的函数。 Is there a standard way to document the type of this parameter? 是否有标准方法来记录此参数的类型?

def example(callback):
    """Example function.

    Parameters
    ----------
    callback : type
       Description of `callback`.
    """
    print(callback(3, 14))

Found the answer while formulating the question, but the question itself doesn't give any results on Google. 在制定问题的同时找到了答案,但问题本身并未在Google上给出任何结果。

PEP-0484 introduces type annotation in python. PEP-0484在python中引入了类型注释。 For example the type of a callback function taking two integers and returning a string is: 例如,采用两个整数并返回字符串的回调函数的类型是:

from typing import Callable

Callable[[int, int], str]

I needed something like that and while I think that callback should always be a function, AFAIK there's no way to specify the type of a function and I kept reusing callbacks with the same arguments doing different things so that's what I came up with. 我需要类似的东西,虽然我认为回调应该始终是一个函数,AFAIK没有办法指定一个函数的类型,我不断重复使用相同的参数执行不同的回调,这就是我想出的。

import abc


class BaseCallback(metaclass=abc.ABCMeta):
    def __init__(self, param: int):
        self.param = param
        self.do_things()

    @abc.abstractmethod
    def do_things(self) -> int:
        raise NotImplementedError


class MyCallback(BaseCallback):
    def do_things(self):
        return self.param * 2


def func(callback: BaseCallback):  # type of callback specified
    callback()


func(callback=MyCallback)

You don't need to run the do_things method in init (imo it's ugly), depends on how much power do you have regarding where/when callback is run. 你不需要在init中运行do_things方法(这很难看),取决于你在回调运行的时间/地点有多少功率。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 PyCharm 解析器的文档字符串中指定类或函数类型 - How to specify class or function type in docstring for PyCharm parser Sphinx docstring:指定类类型而不是对象? - Sphinx docstring: specify class type instead of object? 函数类型和无类型的 NumPy 文档字符串 - NumPy docstring for function type and None type 在python docstring中编写默认值的标准方法是什么? - What is the standard way for writing default values in a python docstring? 如何使用 reST 文档字符串指定可调用参数的返回类型? - How do you specify return type on a callable parameter with reST docstring? 如何在 Python 的函数文档字符串中指定多个返回类型? - How to specify multiple return types in a function docstring in Python? 在函数docstring中记录`tuple`返回类型,用于PyCharm类型提示 - Documenting `tuple` return type in a function docstring for PyCharm type hinting 在函数头或文档字符串中指定输入和输出的类型如何工作? - How does specifying type of input and output in function header or docstring work? 在python中,有没有办法在不覆盖的情况下指定parent(base)function的返回类型? - In python, is there a way to specify the return type of parent(base) function without overriding it? 有没有办法为 python 中带有类型提示的 function 参数指定有效值范围? - Is there a way to specify a range of valid values for a function argument with type hinting in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM