Dave Vogt
Dave Vogt

Reputation: 19602

How can I get the name of a python class?

When I have an object foo, I can get it's class object via

str(foo.__class__)

What I would need however is only the name of the class ("Foo" for example), the above would give me something along the lines of

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

I know I can get it quite easily with a regexp, but I would like to know if there's a more "clean" way.

Upvotes: 1

Views: 222

Answers (3)

Taylor Leese
Taylor Leese

Reputation: 52400

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'
>>>

Upvotes: 1

Vortura
Vortura

Reputation: 1355

foo.__class__.__name__ should give you result you need.

Upvotes: 3

MStodd
MStodd

Reputation: 4746

Try

__class__.__name__

Upvotes: 4

Related Questions