Skip to main content
AmigoFish

The Visitor Pattern in Python (And Better Alternatives)

From ArjanCodes · · 12 min

The player loads from YouTube only when you click. It uses YouTube's privacy-enhanced mode.

If the video does not play, the owner may have turned off embedding. Watch it on YouTube instead.

Watch on YouTube

Not rated yet

Description

đź’ˇ Learn how to design great software in 7 steps: https://arjan.codes/designguide.

The Visitor pattern has a reputation for being complicated, but the problem it solves is actually quite simple: what happens when you keep adding new operations to the same set of objects?

In this video, I use a document editor to compare several ways to solve that problem: methods, pattern matching, the classic Visitor pattern, a dispatch matrix, and Python’s `singledispatch`. More importantly, we’ll look at the trade-offs of each approach and when Visitor is actually worth using.

🔥 GitHub Repository: https://git.arjan.codes/2026/visitor.

đź’¬ Join my Discord server: https://discord.arjan.codes.

⌨️ Keyboard I’m using: https://amzn.to/49YM97v.

đź”– Chapters: 0:00 Intro 1:23 Methods on the Nodes 2:41 Pattern Matching Groups Code by Operation 3:53 Classic Visitor 6:23 The Design Problem 7:06 The Dispatch Matrix Underneath Visitor 9:35 Let Python Maintain the Matrix 11:46 Final Thoughts

#arjancodes #softwaredesign #python

Back to ArjanCodes · Report a problem