You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python trace()模块追踪乘法表执行并输出标注结果

How to Trace Your Multiplication Table Program with Python's trace Module

Got it, let's fix this up so you can actually trace your multiplication table code and get annotated results. The main issue with your current code is that you created a Trace object, but never told it to run your target function—so it wasn't actually tracking any execution!

Here's the corrected, working version of your code, with explanations:

import sys
import trace

# Initialize the tracer: set trace=1 to enable execution tracing, count=1 for coverage stats
tracer = trace.Trace(
    ignoredirs=[sys.prefix, sys.exec_prefix],  # Skip standard library directories
    trace=1,  # Enable line-by-line execution tracing
    count=1   # Track line execution counts for coverage
)

def print_multiples(n, high):
    for i in range(1, high+1):
        print(n * i, end=" ")
    print()

def print_mult_table(high):
    """ Return a multiplication table with n rows and columns."""
    for i in range(1, high+1):
        print_multiples(i, high)

# Use the tracer to run your target function—this is the key step you missed!
tracer.runfunc(print_mult_table, 7)

# Get the results object and write annotated output
results = tracer.results()
# Write coverage stats and annotated source files to the current directory
results.write_results(show_missing=True, coverdir=".")

What's Changed & Why:

  • tracer.runfunc(print_mult_table, 7): This tells the tracer to execute your print_mult_table function with the argument 7, and track every line of code it runs. Without this, your tracer was just sitting idle.
  • trace=1: Enables line-by-line execution tracing, so the module logs which lines are executed when. If you only care about coverage (not the step-by-step flow), you can leave this as 0, but setting it to 1 gives you the execution trace output.

What You'll Get:

  1. Real-time Execution Trace: When you run the script, you'll see output like this (showing each line as it's executed):
    --- modulename: your_script, funcname: print_mult_table
    your_script.py(13):     for i in range(1, high+1):
    your_script.py(14):         print_multiples(i, high)
    --- modulename: your_script, funcname: print_multiples
    your_script.py(8):     for i in range(1, high+1):
    your_script.py(9):         print(n * i, end=" ")
    ... [continues for all lines of the multiplication table execution]
    
  2. Annotated Source Files: After running, you'll find a new file named your_script.cover (replace your_script with your actual filename) in your working directory. This file annotates each line with how many times it was executed, and marks any lines that weren't run (though in this case, all lines will be executed). For example:
    def print_multiples(n, high):
        7       for i in range(1, high+1):
       49           print(n * i, end=" ")
        7       print()
                def print_mult_table(high):
                    """ Return a multiplication table with n rows and columns."""
        7       for i in range(1, high+1):
        7           print_multiples(i, high)
        1       print_mult_table(7)
    
    The numbers on the left are the execution counts for each line.

Bonus: If You Want Detailed Step-by-Step Annotations

If you want even more detailed output (like variable values), you might need to use a custom trace function with sys.settrace, but the trace module's built-in functionality is perfect for basic execution tracing and coverage annotation.

内容的提问来源于stack exchange,提问作者NationalRex22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:06:40