如何获取函数的参数类型和返回类型?

编程入门 行业动态 更新时间:2024-10-25 14:30:27
本文介绍了如何获取函数的参数类型和返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 python 中实现强类型遗传编程.

I'm trying to implement strong type genetic programming in python.

有类似这些样本的吗?

def funcA(a,b): return a + b return_type(funcA) output: <class 'Integer'>

def funcA(a,b): return a + b parameter_type(funcA) output: [<class 'Integer'>,<class 'Integer'>]

更新:

我正在尝试生成 python 的表达式并避免无法像这样评估某些内容:

I'm trying to generate python's expression and avoiding something cannot be evaluated like this:

funcA(20, funcA(True, "text"))

推荐答案

Python 3 引入了函数注解.他们自己什么都不做,但您可以编写自己的执行:

Python 3 introduces function annotations. By themselves they don't do anything, but you can write your own enforcement:

def strict(fun): # inspect annotations and check types on call @strict def funcA(a: int, b: int) -> int: return a + b

更多推荐

如何获取函数的参数类型和返回类型?

本文发布于:2023-07-22 10:36:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   函数   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!