🔥 BLACK FRIDAY SALE1 course ₹199·any 3 courses ₹399 for 3 months·ALL courses + 1 mock interview ₹599 for 6 monthsClaim offer →

Python · Classes and Object Design · Intermediate · 26 min read

Python Composition, Protocols and Duck Typing: Designing with Has-A Instead of Is-A

Design flexible Python code without deep hierarchies. Learn why composition usually beats inheritance, how to delegate and inject collaborators, duck typing and the EAFP style, structural typing with typing.Protocol, the strategy and adapter patterns in Python, mixins and when they are acceptable, __getattr__ delegation, and a refactoring workflow from a class hierarchy to composed parts.

Python Composition, Protocols and Duck Typing: Designing with Has-A Instead of Is-A: lesson illustration

A first look

  • Intermediate level · 26 min read
  • 7 interview questions with model answers
  • Classes and Object Design · Python Mastery

You will learn to

  • The problem with deep inheritance, and what composition offers instead
  • Dependency injection by constructor argument; strategy objects
  • Duck typing, EAFP and structural interfaces with Protocol
  • …and 3 more topics inside
Create an account to start

Create a free account to see what this lesson covers

  • Every free lesson in every section, in full
  • Track your progress and pick up where you left off
  • Free practice questions and an incident simulation
  • Takes under a minute: name, email and a password

No card needed. Full access to Python is a paid upgrade you can choose later.