function get_class_name()

Irmen de Jong irmen at -NOSPAM-REMOVE-THIS-xs4all.nl
Thu Mar 13 17:34:55 EST 2003


username wrote:
> I need something (<<code>>) for a class i.e.
> 
> class Hello:
> 	.
> 	.
> 	def get_class_name(self):
> 		<<code>>
> 
> that does this
> 
> 
>>>>World=Hello()
>>>>World.get_class_name()
> 
>    "World"

You're confusing me by the name of the method "get_class_name".
The class name of the object isn't "World", it's "Hello".
Every instance of "Hello" is of class "Hello":

 >>> class Hello:
...  pass
...
 >>> a=Hello()
 >>> b=Hello()
 >>> a.__class__.__name__
'Hello'
 >>> b.__class__.__name__
'Hello'

The other possible meaning that your desired method might have,
namely, returning the identifier name that's referring to
your object, is *impossible*. Why? Because many names may refer
to the same object!

--Irmen





More information about the Python-list mailing list