How to get class method name in Python?

There are two ways to do this. You can either do:


def whoami():
    """Returns name of the method."""
    import sys
    return sys._getframe(1).f_code.co_name

def callersname():
    """Returns name of the caller."""
    import sys
    return sys._getframe(2).f_code.co_name

Or you can simply do:


import inspect
inspect.stack()[0][3]

This will give you method name. Pretty simple?

About Wolverine

If you are looking for IT consultant, let me know! karol at karoltomala dot REMOVE com Just remove the REMOVE word from the e-mail above!
This entry was posted in Coding, Programming, Python and tagged , , . Bookmark the permalink.

Leave a Reply

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