liveBook · Manning

Accept method

As explained in Software Design for Python Programmers

An accept method is an operation on a tree element that receives a visitor and dispatches the visitor operation appropriate to that element's type.

An accept method lets a tree node receive a visitor, dispatch the visitor operation matching the node type, and continue traversal through child nodes. section 11.2.3

A node-specific accept method typically:

  • Calls the visitor method for its own type, passing itself so the visitor can use the node's public methods. section 11.2.3
  • Delegates to child nodes, keeping collection structure and traversal out of visitor classes. section 11.2.3
  • Uses polymorphism to bind node and visitor types at run time. section 11.2.3

Intramural node

An Intramural node calls visitor.visit_intramural(self), then asks each Sport and Hall child to accept the same visitor. Sport similarly calls visit_sport(self) and visits its Game children. section 11.2.3

Listing 11.22 (Program 11.4 Results-VisitorDP) intramural.py
from node import Node

class Intramural(Node):
    ...

    def accept(self, visitor):
        visitor.visit_intramural(self)    #1

        for sport in self._sports:
            sport.accept(visitor)    #2

        for hall in self._halls:
            hall.accept(visitor)    #3

Shows an accept implementation that dispatches to the node-specific visitor method and recursively visits child collections. section 11.2.3

How Software Design for Python Programmers covers it

How deep it goes
Explains runtime dispatch, self-passing, recursive traversal, and structural encapsulation through concrete node implementations.
Examples
Uses Intramural, Sport, Game, and Hall nodes with scores, activities, and winnings report visitors.
What it assumes
Assumes familiarity with tree nodes, interfaces, visitors, and basic polymorphic method calls.
What you can do after
Build it from scratch in Python with node-specific dispatch and recursive visitor traversal.
How it connects
Connects Visitor pattern, polymorphism, interfaces, tree data structures, and report generation algorithms.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.