🐍 Python Advanced

What is Python's design patterns?

Why Interviewers Ask This

Senior Python engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.

Answer

Python's dynamic nature means many classic design patterns are simpler or unnecessary. Singleton: use module-level globals (modules are singletons) or a metaclass. Factory: simply a function or class method returning instances. Observer: Python callbacks, signals libraries, or property setters. Strategy: pass callables/lambdas as arguments — functions are first-class. Decorator: Python's @decorator syntax is specifically designed for this. Iterator: implement __iter__/__next__ or use generators. Context Manager: with statement and contextlib. Command: callables and functools.partial. Composite: Python's duck typing makes this natural. Proxy: __getattr__ forwarding. Pythonic alternatives often replace GOF patterns: protocols replace interfaces, first-class functions replace Strategy/Command, decorators replace Proxy/Wrapper. Focus on idiomatic Python (duck typing, EAFP) rather than forcing Java-style patterns.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.