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.
Python code
43 linesclass 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
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
- Use an event bus or observer pattern to decouple binding further, allowing multiple Views to share one ViewModel.
- 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
More from System design patterns
- Build a BFF (Backend for Frontend) Mock Aggregator in Python medium
- Builder pattern for mocking complex objects in Python easy
- Circuit Breaker Pattern in Python: Closed, Open, and Half-Open States medium
- Create a Data Helper Class in Python easy
- Domain Driven Design Aggregate Root Example in Python medium
- Facade Pattern in Python with Mock Simplification medium
Keep learning
Related tutorials and quizzes for this topic.