Python Glossary Русский

Python 3.9+pstatsterm

pstats.StatsProfile

programmatic access

A dataclass with a whole profile: the total time total_tt and a func_profiles dict mapping function name to FunctionProfile.

Syntax

sp = pstats.Stats(profile).get_stats_profile()

It is the only way to get profile results as data rather than printed text — for instance, to assert in a test that a function ran at most N times. The dict key is just the function name, without class or file: A.run and B.run collapse into a single 'run' entry, and which one survives depends on the version. Built-ins appear under long names such as "<built-in method builtins.len>".

Examples

import cProfile, pstats
class A:
    def run(self): pass
class B:
    def run(self): pass
with cProfile.Profile() as pr:
    A().run()
    B().run()
sp = pstats.Stats(pr).get_stats_profile()
print(type(sp).__name__)  # → StatsProfile
print([name for name in sp.func_profiles if 'run' in name])  # → ['run']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy