class name lookup

Geir Bjarte Terum gterum at c2i.net
Thu Oct 28 19:05:16 EDT 1999


I'm wondering if there is any better way to look up a class name at run
time than what I've come up with this far. I wan't to retrieve the
instance class name from within base class.

Example:

class parent:
  def __init__(self):
    print 'class name = ', str(self)   # or repr(self)

class chield(parent):
  def __init__(self):
    parent.__init__(self)

if __name__=='__main__':
  c = chield()

The result here is something like:
"class name = <__main__.chield instance at 797210>"

But I would like something like:
"class name == chield"   # Even "__main__.chield" may do fine.

If I can rely on that the format returned from str() or repr() is
platform independent and konsistent in time, I can always strip down the
string as I please. Anyway, is there any other more natural way of doing
this?

Regards
- gb -





More information about the Python-list mailing list