On this page
  1. pyclbr
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Walking Class Definitions and Methods
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects

pyclbr

Prev: py_compile | Up: stdlib_module | Next: pydoc
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — pyclbr
Tags: #public #python3 #stdlib


Import

import pyclbr

Overview

Lightweight static class and function browser (readmodule(), readmodule_ex()) that parses Python source files without importing or executing them. Use pyclbr to extract top-level class hierarchies, method names, and line numbers safely; for full syntax analysis (type annotations, decorators, expressions), prefer ast.

Quick Start

import pyclbr

# Inspect top-level classes and functions in a stdlib module without executing it
tree = pyclbr.readmodule_ex("colorsys")
assert "rgb_to_hsv" in tree
assert isinstance(tree["rgb_to_hsv"].lineno, int)

Implementation Walkthrough

1. Walking Class Definitions and Methods

Class descriptors returned by readmodule_ex() expose .name, .super, .methods (mapping method name → line number), .lineno, and .children:

import pyclbr

tree = pyclbr.readmodule_ex("cmd")
cmd_cls = tree["Cmd"]
assert isinstance(cmd_cls, pyclbr.Class)
assert "cmdloop" in cmd_cls.methods

Gotchas & Boundaries

Common Functions

Classes & Objects

Linked from

Related notes