Getting a code obj's function?

Fredrik Lundh fredrik at
Fri Dec 15 10:35:17 CET 2000

Bjoern Giesler wrote:
> there a standard way to get the function object that a code object is
> associated with? On exception, I only get the code object from the
> traceback.

Use co_name:

    def spam():

    >>> spam
    <function spam at 9281b8>

    >>> spam.func_code
    <code object spam at 91cd08, file "<stdin>", line 1>

    >>> spam.func_code.co_name

For more ideas on what you can do with frames and code objects, check
the source code for the traceback module (in the standard library).


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list