In Python 3.5, how can I specify a function as a type hint?

空扰寡人 提交于 2019-12-19 19:49:33

问题


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#)?

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.


回答1:


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

You can view more details in PEP 0484.



来源:https://stackoverflow.com/questions/35315824/in-python-3-5-how-can-i-specify-a-function-as-a-type-hint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!