| id |
behavioral/visitor |
| name |
Visitor |
| aliases |
|
| guide_url |
|
| problem |
Run a new operation over every node of an object structure without adding a method to every node class. |
| symptoms |
walk an AST |
operation per node type |
double dispatch |
add behavior across a class family |
|
| verdict |
prefer-alternative |
| caveats |
functools.singledispatch dispatches on type without touching the node classes — the visitor with the accept() plumbing deleted. |
The subclass form survives where the stdlib hands it to you: ast.NodeVisitor is the right tool for walking Python source. |
|
| stdlib_sightings |
functools.singledispatch |
ast.NodeVisitor |
|
New operations over a node structure, without editing the nodes. Verdict:
prefer an alternative — singledispatch deletes the accept() plumbing;
the subclass form survives at stdlib boundaries (ast.NodeVisitor).
uv run python -m patterns.behavioral.visitor.examples.doc_exporters.main