2

I apologize if my terminology is incorrect. I am trying to get the name of the function from an API return. For example, the following is what is returned from an API. How do I get the name, the_function?

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

The type of the above is:

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

If I only have access to what I have shown, how to I the text string the_function? Is there an easy way to do it besides turning it into a string and using regex or something similar?

khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

1

Use __name__ argument:

def yourfunction():
    pass

print(yourfunction.__name__)

Then now you'll get expected output.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

If you have access to that function through literally any variable, you can use .__name__:

 >>> def the_function():
...     pass
... 
>>> the_function.__name__
'the_function'
>>> foo = the_function
>>> foo.__name__
'the_function'
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93