12

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,742
  • 8
  • 31
  • 41
  • 1
    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

30

Try this:

instance.__class__.__name__
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306