gpt4 book ai didi

python - 如何解析类属性的名称?

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:01 25 4
gpt4 key购买 nike

如果我的术语不正确,我深表歉意。我试图从 API 返回中获取函数的名称。例如,以下是从 API 返回的内容。如何获取名称 the_function

my_variable = <Function the_function(str,int,uint)>

上面的类型是:

type(my_variable) = <class 'the_class.utils.datatypes.the_function'>

如果我只能访问我显示的内容,如何访问文本字符串 the_function?除了将其转换为字符串并使用正则表达式或类似的东西之外,还有其他简单的方法吗?

最佳答案

如果您可以通过任何变量访问该函数,您可以使用 .__name__:

 >>> def the_function():
... pass
...
>>> the_function.__name__
'the_function'
>>> foo = the_function
>>> foo.__name__
'the_function'

关于python - 如何解析类属性的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54035511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com