Exploring Duck Typing
00:00 In this lesson, you’ll learn what duck typing is and see it in action.
00:05 Let’s begin with one of the core principles of object-oriented programming: classes. Classes are all about encapsulating data and behaviors. They define what an object can do and how it interacts with the world.
00:18 Also, you don’t actually need to know what class an object belongs to. As long as an object provides the methods or attributes you’re looking for, you can swap it out with another object that behaves the same way.
00:30 This very idea that behavior matters more than type brings you to duck typing. Duck typing is a system where an object is treated as a certain type if it has the required methods and attributes regardless of its actual class.
00:46 This allows objects from different unrelated classes to work in a specific situation as long as they follow the same basic rules or interface.
00:57 The name “duck typing” actually comes from the saying, if it walks like a duck, quacks like a duck, then it’s a duck. In programming, the concept of duck typing encourages you to think about objects in terms of what they do rather than what they are.
01:13 So you don’t need to ask, is this a duck? Instead, you just check if the object can quack and walk like a duck. If it can, you treat it as one. Quacking and walking symbolize the actions or behaviors an object can perform.
01:28 Let’s look at an example to understand this principle.
01:32
You have three classes: Duck
, Swan
, and Albatross
. Each class defines the same two methods, swim()
and fly()
, with its own specific implementation.
01:44
In the Duck
class, you have the swim()
method, which says the duck is swimming, and a fly()
method, which says the duck is flying.
01:52
Similarly, the Swan
class has methods that say what the swan is doing, and so does the Albatross
class. The important thing here is that these three classes don’t share a common parent class or interface.
02:05
They’re completely independent, but because each class implements the same two methods, which are swim()
and fly()
, Python can handle them in the same way.
02:15 This is where duck typing comes in.
02:18
Take a look at the second snippet. You’re creating a list called birds
, which contains objects of Duck()
, Swan()
, Albatross()
.
02:27
Next, you’re iterating over the list and calling the fly()
and swim()
methods on each object. And as you can see, Python doesn’t care what type each object is, as long as the object has a fly()
method and a swim()
method your code works perfectly. And this is duck typing in action.
02:45 When you run this code, here’s the output you’ll see: the duck is flying, the duck is swimming, the swan is flying, the swan is swimming, and finally, the albatross is flying and the albatross is swimming.
02:57
This shows you that Python is not checking if an object is specifically a Duck
, Swan
or Albatross
. It’s only checking if the object can fly and swim.
Become a Member to join the conversation.