Getting Object Name And Function Name
It is actually 2 questions. 1) Is there a generic way to get the class name of an instance, so if I have a class class someClass(object): I would like a built in way that gives m
Solution 1:
use the __name__
attribute:
Class:
>>>classA:pass>>>A.__name__
'A'
>>>A().__class__.__name__ #using an instance of that class
'A'
Function:
>>>deffunc():...print func.__name__...>>>func.__name__
'func'
>>>func()
func
A quick hack for classes will be:
>>>import sys>>>classA():...deffunc(self):... func_name = sys._getframe().f_code.co_name... class_name = self.__class__.__name__...print'Executing {} from {}'.format(func_name, class_name)...>>>A().func()
Executing func from A
Solution 2:
the function part has already been answered at this SO post. The code would be:
import sys
print sys._getframe().f_code.co_name
For the class part, use: A.__name__
or A().__class__.__name
(for an instance)
Post a Comment for "Getting Object Name And Function Name"