I know Python introspection feature are powerful, what I can't seem to figure out is how to get the name of the current class.

Example:

class Foo:
   def bar(self):
        print(classname)

Then my output should be Foo. Bonus points if I can get which module it resides in too.
Thanks!

Recommended Answers

All 3 Replies

>>> class Thing(object):
	pass

>>> t = Thing()
>>> dir(t)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
>>> print t.__class__
<class '__main__.Thing'>

>>> print t.__module__
__main__

Jeff

Thanks Jeff, exactly what I was looking for.

I know Python introspection feature are powerful, what I can't seem to figure out is how to get the name of the current class.

Example:

class Foo:
   def bar(self):
        print(classname)

Then my output should be Foo. Bonus points if I can get which module it resides in too.
Thanks!

The Python module inspect should contain your desired functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.