How to Implement a Simple MVVM Binding Mock in Python

A minimal Python implementation of the MVVM pattern, mocking data binding so views auto-update when the view model changes.

Medium Python 3.9+ Aug 9, 2026 System design patterns 18 views 0 copies

Python code

43 lines
Python 3.9+
class BindingMock:
    def __init__(self, view_model):
        self.view_model = view_model
        self.subscribers = []

    def bind(self, property_name, callback):
        self.subscribers.append((property_name, callback))

    def set(self, property_name, value):
        setattr(self.view_model, property_name, value)
        self.notify(property_name)

    def notify(self, property_name):
        for bound_name, callback in self.subscribers:
            if bound_name == property_name:
                callback(getattr(self.view_model, property_name))


class ViewModel:
    def __init__(self):
        self.user_name = ""
        self.age = 0


class View:
    def __init__(self, view_model):
        self.view_model = view_model
        self.binding = BindingMock(view_model)
        self.binding.bind("user_name", self.update_name_label)
        self.binding.bind("age", self.update_age_label)

    def update_name_label(self, value):
        print(f"Name label updated to: {value}")

    def update_age_label(self, value):
        print(f"Age label updated to: {value}")


if __name__ == "__main__":
    vm = ViewModel()
    view = View(vm)
    view.binding.set("user_name", "Alice")
    view.binding.set("age", 30)

Output

stdout
Name label updated to: Alice
Age label updated to: 30

How it works

The BindingMock class stores subscriber callbacks and triggers them when the set method updates a property. This simulates the bidirectional data binding found in frameworks like WPF or Vue. By decoupling the view from the view model, the pattern improves testability and separation of concerns. The mock avoids UI framework dependencies, making it ideal for identifying edge cases before integrating a real binding library.

Common mistakes

  • Forgetting to call notify after setattr, leaving the UI stale.
  • Binding the same property multiple times without deduplication.
  • Assuming set updates nested objects or lists without deep copy semantics.

Variations

  1. Use an event bus or observer pattern to decouple binding further, allowing multiple Views to share one ViewModel.
  2. Implement property descriptors in the ViewModel to automatically trigger callbacks on any attribute assignment.

Real-world use cases

  • Prototyping a desktop application's UI behavior without tying it to a GUI toolkit during early development.
  • Testing that a view model emits the right notifications when state changes, before integrating a production binding framework.
  • Teaching MVVM fundamentals in a training context where the focus is on architecture, not framework specifics.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from System design patterns

Related tutorials and quizzes for this topic.