Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:33:50+00:00 2026-06-12T13:33:50+00:00

A Python function has a code object __code__ . A sys.settrace trace frame has

  • 0

A Python function has a code object __code__.

A sys.settrace trace frame has a f_code code object.

For those calls to the tracer that are functions, how can I get the function object (and its __annotation__ member)?

So far, by trial and error, I have:

if hasattr(frame.f_globals.get(frame.f_code.co_name),"__annotations__"):

This seems to work for functions, but not for class-member functions; worse, it confuses class-member functions with top-level functions of the same name.

(I’m on Python 3.2.3 (Xubuntu). I see that Python 3.3 inspect module has a signature function; will this return the annotation for a code object or does it too need a function object?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:33:51+00:00Added an answer on June 12, 2026 at 1:33 pm

    Through the inspect.getframeinfo module.
    I mean — there is no straightforward way of doing that in Python — Most times you can get hold of the code object, without having the function already, it is through frame instrospection.

    Inspect’s getframeinfo function does return some information about the frame being run, then you can retrieve the function object by getting its name.

    Tough this is implementation dependent and have some drawbacks:

    >>> import inspect
    >>> def a():
    ...   return inspect.currentframe()
    ... 
    
    >>> inspect.getframeinfo(a())
    Traceback(filename='<stdin>', lineno=2, function='a', code_context=None, index=None)
    >>> b = inspect.getframeinfo(a())
    >>> b.function
    'a'
    

    Another way, but still implementation dependent, is to use the gc module (garbage collector) to get referrers to said code object.

    >>> import gc
    >>> from types import FunctionType
    >>> def a(): pass
    ... 
    >>> code = a.__code__
    
    >>> [obj for  obj in  gc.get_referrers(code) if isinstance(obj, FunctionType)  ][0]
    <function a at 0x7f1ef4484500>
    >>> 
    

    —
    This is for Python 3 – for Python 2 one should replace __code__ by func_code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a neural network object in python that has a cost function and
A Python function object has an attribute dictionary called func_dict which is visible from
I'm trying to run python code that has an inheritence pattern like so: A
I have a Python object which has certain attributes that are set after the
I am working on some Python socket code that's using the socket.fromfd() function. However,
This code generates AttributeError: 'Popen' object has no attribute 'fileno' when run with Python
I want a python function that takes a pdf and returns a list of
I have a python function that makes a subprocess call to a shell script
I have a python function that randomize a dictionary representing a position specific scoring
I understand the following Python code: >>> class A(object): ... def __str__(self): ... return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.