carpetemporem
carpetemporem

Reputation: 191

How to get the qualified name of the currently executing function?

Python 3.3 introduced the __qualname__ attribute for function objects and class objects.

It's easy to get the (unqualified) name and a code object for the currently executing function.

But how to get the qualified name for the currently executing function?

Upvotes: 5

Views: 1218

Answers (3)

drammock
drammock

Reputation: 2543

As of Python 3.11 this is now possible via:

inspect.currentframe().f_code.co_qualname

(with optional .f_backs inserted before the .f_code as desired)

https://docs.python.org/3.11/reference/datamodel.html#codeobject.co_qualname

Upvotes: 1

Alex Hall
Alex Hall

Reputation: 36043

You can use executing:

executing.Source.for_frame(frame).code_qualname(frame.f_code)

Upvotes: 2

ecatmur
ecatmur

Reputation: 157484

I don't think you can, currently; see this thread.

Issue 13672 requests adding co_qualname to code objects, and issue 12857 requests making the called function available through the frame object. Both have patches attached.

Upvotes: 1

Related Questions