1. Home
  2. Explore
  3. Computer Science
  4. Design Patterns in Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Design Patterns in Python

Speaker 1Speaker 2

Design Patterns in Python

2:29
0:00-2:29
  1. Simon

    I keep hearing people say that design patterns from the Gang of Four book are obsolete, especially in Python. The argument is that you can just use functions instead of classes and get simpler code.

  2. Anna

    Right, I've heard that too. And sure, functions can often replace the class-based implementations. But I don't think that makes the patterns themselves obsolete. The real value is in recognizing the type of problem you're dealing with.

  3. Simon

    Exactly. Take the Strategy pattern. In the classic object-oriented version, you have an abstract strategy class, concrete subclasses for different algorithms, and a context class that uses the strategy. But in Python, you can just pass a function as an argument.

  4. Anna

    And that gives you way less code. The same behavioral decoupling without the boilerplate. The core idea—being able to swap algorithms without the client knowing—is what matters, not the class structure.

  5. Simon

    Same with the Observer pattern. Normally you'd have an abstract observer class with an update method, concrete observers, and a subject that maintains a list. But you can just use a list of functions.

  6. Anna

    So instead of attaching observer objects, you pass a list of callables to a notify function. That's decoupling without the overhead. And you can even go further by combining data structures like dictionaries mapping events to listeners.

  7. Simon

    Then there's the Template Method pattern. The classic version uses an abstract base class with a template method that calls abstract operations and optional hooks. Subclasses implement the required parts. But you can do it functionally.

  8. Anna

    You just define a template function that takes the required operations as arguments, with optional hooks as default parameters. That gives you the same structure with about 20 fewer lines of code.

  9. Simon

    And you can spice it up by returning a function from a function—like a closure that captures the operations, so you only pass the hooks later. Or use `functools.partial` to pre-fill arguments.

  10. Anna

    Both approaches give you the same flexibility as inheritance, but with less ceremony. So while the implementation changes, the pattern—the problem category—remains constant.

  11. Simon

    That's the key takeaway: design patterns are about recognizing problem classes like 'I need to swap algorithms' or 'I need to notify multiple listeners without coupling.' How you implement it—classes, functions, or a mix—is secondary.

  12. Anna

    So don't throw out the design patterns book. Instead, learn the problems it describes, and then choose the best tools in your language to solve them. In Python, functions are often a great choice, but the pattern knowledge is what makes you a better developer.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent