在Python 3.5中,如何将函数指定为类型提示? [英] In Python 3.5, how can I specify a function as a type hint?

查看:142
本文介绍了在Python 3.5中,如何将函数指定为类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是合适的类型提示以指定变量应为函数(相当于C#中的委托,Func<T>Action)?

What is the appropriate type hint to specify that a variable should be a function (the equivalent of a delegate, Func<T> or Action in C#)?

是否也可以以通用方式指定函数参数类型(例如Func<int, int>)?

Is it also possible to specify the function argument types in a generic fashion as well (for example Func<int, int>)?

我在文档中找不到任何相关详细信息.

I cannot find any relevant details in the documentation.

推荐答案

根据 docs ,您可以使用通过键入导入的Callable.

According to the docs, you can use Callable, imported from typing.

您可以在 PEP 0484 中查看更多详细信息.

You can view more details in PEP 0484.

这篇关于在Python 3.5中,如何将函数指定为类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆