Introspection Class/Instance Name

robert no-spam at no-spam-no-spam.com
Wed Apr 26 08:38:47 EDT 2006


*binarystar* wrote:
> Hello there,
> 
> what method would you use to return the name of the class and/or 
> instance introspectively eg.
> 
> class Bollocks:
>     
>     def __init__( self ):
>        
>         print self.__method_that_returns_class_name__()
>         print self.__method_that_returns_instance_name__()
> 
> 
> instance_of_bollocks    = Bollocks()
> 
> # Which outputs
> 
> 'Bollocks'
> 'instance_of_bollocks'
> 

self.__class__ is good for getting instance's top class

yet Python is weak on introspecting the namespace of its definitions - 
funcs and classes. it stops at sys._getframe().f_code.co_name

thus, e.g. for things like super() you need always to re-type the class 
name where you just write in!?

maybe Py3K brings more ? Maybe There could be compiler-variables like
__CLASS__ ,  __FUNC__ for things like super(), recursion etc.
(compare http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491265 )

-robert



More information about the Python-list mailing list