7

I'm probably overlooking something simple. Given an instance of a class, I'd like to get just the class name. For example:

class Foooo: pass
instance = Foooo()

print("instance.__class__ = "+str(instance.__class__))
print("Just the class name: "+str(instance.__class__).split(".")[-1][:-2])

This gives the following output:

instance.__class__ = <class '__main__.Foooo'>
Just the class name: Foooo

Is there something simpler than

str(instance.__class__).split(".")[-1][:-2]?

I'm in Python 3.2 if that helps...

Matthew Lund
  • 3,362
  • 6
  • 28
  • 39
  • This is a duplicate of http://stackoverflow.com/q/510972/2099613. You can find more explanations including the preferred solution `type(instance).__name__` there – yanlend Sep 01 '14 at 13:26

1 Answers1

25

Try this:

instance.__class__.__name__
Andrew Clark
  • 192,132
  • 30
  • 260
  • 294