How can I get the name of the python class?

When I have a foo object, I can get the class object via

str(foo.__class__)

      

However, I only need the class name (eg "Foo"), the above would give me something like strings

"<class 'my.package.Foo'>"

      

I know I can get this pretty easily with a regex, but I would like to know if there is a "cleaner" way.

+1


a source to share


3 answers


Try



__class__.__name__

      

+4


a source


foo .__ class __.__ name__ should give you the result you want.



+3


a source


Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo:
...     x = 1
...
>>> f = foo()
>>> f.__class__.__name__
'foo'
>>>

      

+1


a source







All Articles