Python Tricks: : Accessing class and function names at runtime

# You can get the name of
# an object's class as a
# string:

>>> class MyClass: pass

>>> obj = MyClass()
>>> obj.__class__.__name__
'MyClass'

# Functions have a
# similar feature:

>>> def myfunc(): pass

>>> myfunc.__name__
'myfunc'

Leave a Reply

Your email address will not be published. Required fields are marked *