Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Python - inspect - Get full caller name (package.module.function)
# Public Domain, i.e. feel free to copy/paste
# Considered a hack in Python 2
import inspect
def caller_name(skip=2):
"""Get a name of a caller in the format module.class.method
`skip` specifies how many levels of stack to skip while getting caller
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
An empty string is returned if skipped levels exceed stack height
stack = inspect.stack()
start = 0 + skip
if len(stack) < start + 1:
return ''
parentframe = stack[start][0]
name = []
module = inspect.getmodule(parentframe)
# `modname` can be None when frame is executed directly in console
# TODO(techtonik): consider using __main__
if module:
# detect classname
if 'self' in parentframe.f_locals:
# I don't know any way to detect call from the object method
# XXX: there seems to be no way to detect static method call - it will
# be just a function call
codename = parentframe.f_code.co_name
if codename != '<module>': # top level usually
name.append( codename ) # function or a method
del parentframe
return ".".join(name)

mgedmin commented Feb 28, 2013

This is exactly why I was overjoyed when appeared. Python 3.3+ only, though.

Otherwise the only sure way to figure out the class name I can think of would be using parentframe.f_code.co_filename and co_firstlineno, then parsing the source file (if it's available) and seeing what classes where defined in which code lines.


techtonik commented Feb 28, 2013

__qname__ sounds better to me, but it still it doesn't solve the problem of orientation in the frame completely. For example, what is the convention to identify the top level? I understand that this top level differs depending on how the code is called - usual 'python', console, exec() - what else is possible? What is the difference? Why it should be different? These questions I don't have answers for now.

Very useful, thank you.


👏 thanks so much for this!

Safihre commented Oct 15, 2017

6 years later, but still want to say thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment