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

  • Home
  • SEARCH
  • 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 421147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:57:17+00:00 2026-05-12T18:57:17+00:00

Is there a way to get the trace table for a Python program? Or

  • 0

Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I’m a teacher trying to flawlessly verify the answers to the tracing problems that we use on our tests.

So, for example, assuming I have a Python program named problem1.py with the following content:

problem1.py

 a = 1
 b = 2

 a = a + b

Executing the presumed program traceTable.py should go as:

 $ python traceTable.py problem1.py
 L || a | b
 1 || 1 |
 2 || 1 | 2
 4 || 3 | 2

(Or the same information with a different syntax)

I’ve looked into the trace module, and I can’t see a way that it supports this.


Updated

Ladies and gentlemen: using Ned Batchelder’s excellent advice, I give you traceTable.py!

Well.. almost. As you can see in Ned Batchelder’s example, frame.f_lineno doesn’t always behave intuitively (e.g. both lines 3 & 4 are counted as line 4), but the line numbers are close enough for a fairly good reference. Also, all calculations are correct.

I have tested this with a long program containing an if statement and it gave the correct table (sans the line numbers).

You will also notice that my program is significantly longer than Ned Batchelder’s proof of concept due to accounting for the “more interesting ecosystems of data” in larger programs he mentioned. In the scope of using execfile and all the variables needed to manage it and reduce noise (ala ignored_variables) as well as produce proper string output, a lot more code is needed:

traceTable.py

 '''
 Usage: python traceTable.py program

     -program  Python program to be traced
 '''

 import sys

 if len(sys.argv) < 2:
      print __doc__
      exit()
 else:
      file_name = sys.argv[1]

 past_locals = {}
 variable_list = []
 table_content = ""

 ignored_variables = set([
      'file_name',
      'trace',
      'sys',
      'past_locals',
      'variable_list',
      'table_content',
      'getattr',
      'name',
      'self',
      'object',
      'consumed',
      'data',
      'ignored_variables'])

 def trace(frame, event, arg_unused):
      global past_locals, variable_list, table_content, ignored_variables
      relevant_locals = {}
      all_locals = frame.f_locals.copy()
      for k,v in all_locals.items():
           if not k.startswith("__") and k not in ignored_variables:
                relevant_locals[k] = v
      if len(relevant_locals) > 0 and past_locals != relevant_locals:
           for i in relevant_locals:
                if i not in past_locals:
                     variable_list.append(i)
           table_content += str(frame.f_lineno) + " || "
           for variable in variable_list:
                table_content += str(relevant_locals[variable]) + " | "
           table_content = table_content[:-2]
           table_content += '\n'
           past_locals = relevant_locals
      return trace

 sys.settrace(trace)

 execfile(file_name)

 table_header = "L || "
 for variable in variable_list:
      table_header += variable + ' | '
 table_header = table_header[:-2]
 print table_header
 print table_content

When called, it produces the output

 $ python traceTable.py problem1.py
 L || a | b
 2 || 1
 4 || 1 | 2
 4 || 3 | 2
  • 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-05-12T18:57:17+00:00Added an answer on May 12, 2026 at 6:57 pm

    This isn’t a use case that the current Python tracing tool support, but it should be possible to build. I don’t know how you decide what columns to output. In your sample, a and b are the only local variables, but larger programs would have more interesting ecosystems of data.

    Updated: here’s a simple proof of concept:

     1     import sys
     2
     3     def trace(frame, event, arg_unused):
     4         print event, frame.f_lineno, frame.f_locals
     5         return trace
     6
     7     sys.settrace(trace)
     8
     9     def foo():
    10         a = 1
    11         b = 2
    12
    13         a = a + b
    14
    15     foo()
    

    when run, the output is:

    call 9 {}
    line 10 {}
    line 11 {'a': 1}
    line 13 {'a': 1, 'b': 2}
    return 13 {'a': 3, 'b': 2}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 224k
  • Answers 224k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your factory and your classes are in the same… May 13, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer To do this, you have to edit the default template… May 13, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer It's called augmented reality, and there's numerous libraries and toolkits… May 13, 2026 at 12:49 am

Related Questions

I have some code that throws causes syncdb to throw an error (because it
What is the best way to get stored procedure useage data on a specific
OK I keep getting this error after about 3-4 minutes of churning: Timeout expired.
I been playing around with my code and I wanted to see what would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.