Getting a code obj's function?

Fredrik Lundh fredrik at effbot.org
Fri Dec 15 04:35:17 EST 2000


Bjoern Giesler wrote:
> ...is 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():
        pass

    >>> spam
    <function spam at 9281b8>

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

    >>> spam.func_code.co_name
    'spam'

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).

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list