加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:21:49 所属栏目:Python 来源:网络整理
导读:我正在尝试在 python中实现强类型的遗传编程. 有这样的样品吗? def funcA(a,b): return a + breturn_type(funcA)output: class 'Integer' 和 def funcA(a,b): return a + bparameter_type(funcA)output: [class 'Integer',class 'Integer'] 更新: 我正在尝
我正在尝试在 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的表达式,并避免无法像这样评估的东西:

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

解决方法

Python 3引入了函数注释.他们自己不做任何事情,但你可以写自己的执法:

def strict(fun):
    # inspect annotations and check types on call

@strict
def funcA(a: int,b: int) -> int:
    return a + b

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读