繁体   English   中英

在 python 中键入 Class 方法 __init__ 我对 1 属性的参数类型注释是:Callable = None,注释 state 是什么?

[英]In python typing in the Class method __init__ the argument type annotation I have for 1 attribute is: Callable = None, what does the annotation state?

Class 中的init function 注释如下:

    def __init__(self, directory: str, transforms: Callable = None, extension: str = '.jpg'):

问题是Callable = None指的是什么。

按照惯例,如果transforms - 参数注释意味着接受一个 Callable(即函数),那么需要定义输入参数以及 output,例如它可以是: transforms: Callable[[int,int], int]其中[int,int] would be the function parameters as input, and the latter int` 将是返回值。 但在这里情况并非如此。

在这种情况下, Callable注释期望什么作为输入和返回?

根据Python 文档

一个普通的 Callable 等价于 Callable[..., Any],然后又等价于 collections.abc.Callable。

在没有任何关于 class 的信息或 class 做什么的情况下,很难说出这个 function 和 output 会接受什么作为输入。 这完全取决于 class 的作用,您可能应该阅读 class 的文档。
通过查看extension的默认值, class 需要.jpg文件。 如果它是像pillow这样的图形或图像处理库,则可调用对象可能需要获取某种图像数据。
然而,正如我所说,没有更多信息就无法判断。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM