在Python中,如何获得函数的名称作为字符串?如何获取函数的名称作为字符串?
我想将str.capitalize()
函数的名称作为字符串。看起来该函数具有__name__
属性。当我做
print str.__name__
我得到这个输出,符合市场预期:
str
但是当我运行str.capitalize().__name__
我得到一个错误,而不是让名称为“利用”的。
> Traceback (most recent call last):
> File "string_func.py", line 02, in <module>
> print str.capitalize().__name__
> TypeError: descriptor 'capitalize' of 'str' object needs an argument
同样,
greeting = 'hello, world'
print greeting.capitalize().__name__
给出了这样的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__name__'
出了什么问题?
这有什么错'str.capitalize .__ name__'或'greeting.capitalize。 __name__'? –
返回错误 –
如果你想要变量的名字,你必须单独发现它https://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string python –