Get just the class name without module, etc.

I will probably miss something simple. Given an instance of a class, I would like to get only the class name. For instance:

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 result:

 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 this helps ...

+4
source share
1 answer

Try the following:

 instance.__class__.__name__ 
+15
source

Source: https://habr.com/ru/post/1384399/


All Articles