Loading video player…

Examining Duck Typing and Polymorphism

00:00 In this lesson, you get to know what polymorphism is and how duck typing is one of the ways you can implement it.

00:07 Let’s start with polymorphism. The word might sound fancy, but the idea is straightforward. Polymorphism lets a function handle objects of any type, as long as they share the same behavior, and it adapts its behavior based on the object’s capabilities.

00:22 You’ll find different forms of polymorphism in object-oriented programming. For example, you’ve probably seen polymorphism being implemented using inheritance before.

00:32 Let’s go back to the example that you just saw in the previous lesson. The albatross, the duck, and swan that could fly and swim. Using inheritance, you can create a base class Bird that enforces the subclasses to have the methods swim() and fly().

00:50 If the subclasses don’t have these methods, you’d get a NotImplementedError. Then you can create subclasses of Bird like Duck, Swan, and Albatross that have their own swim() and fly() methods, like Duck would say the duck is swimming.

01:06 In other words, you’re using a base class to create an interface here.

01:12 When you go ahead and create instances of Duck, Swan, and Albatross, they all have fly() and swim() methods since they come from the same base class Bird.

01:22 When you use fly() and swim() on them, it works. The duck is flying, the duck is swimming, etc.

01:30 Duck typing is another way to achieve polymorphism in Python that doesn’t require inheritance. It works by allowing different objects to use the same methods as long as they define them.

01:43 You’ve actually seen this example before in the previous lessons. You’re doing exactly the same thing as before, but without creating any base class and inheriting from it.

01:52 You have separate classes here that don’t share any base classes, yet they all have fly() and swim() methods. Since they all have the needed methods, you can treat them exactly the same.

02:06 When you create instances and use fly() and swim() on them, it works and you get exactly the same results as before. This is implementing polymorphism using duck typing in action.

02:20 Duck typing isn’t just a cool concept. It actually makes your code more flexible and easier to work with, and here’s why. Flexibility: you can use different objects interchangeably as long as they have the right behavior.

02:33 No need to worry about their exact types. Simplicity: Instead of focusing on what class an object belongs to, you just care about what it does. Code reuse: you can reuse classes in different contexts without forcing them into a rigid inheritance structure.

02:50 Easier prototyping: You can create objects with necessary behavior and skip complex type definitions.

02:59 But of course, duck typing isn’t perfect. Here’s what to watch out for: runtime errors. If an object is missing a required method, your code won’t fail until execution time.

03:11 Python won’t warn you in advance. Lack of explicitness: without clear type definitions, it can be harder to tell which methods an object should have just by looking at it.

03:24 Maintenance issues: as your project grows, keeping track of which object supports which behavior can get really tricky. Now, you’ll see concrete examples of these downsides and the alternatives soon, but for now, the key takeaway is this—duck typing makes your code more dynamic, but it also requires careful handling to avoid unexpected errors.

Become a Member to join the conversation.