In Python, how do I get a function's name as a string?
I want to get the name of the str.capitalize()
function as a string. It appears that the function has a __name__
attribute. When I do
print str.__name__
I get this output, as expected:
str
But when I run str.capitalize().__name__
I get an error instead of getting the name "capitalize".
> 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
Similarly,
greeting = 'hello, world'
print greeting.capitalize().__name__
gives this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__name__'
What went wrong?