Loading video player…

Getting Started With Duck Typing (Overview)

Python makes extensive use of a type system known as duck typing. The system is based on objects’ behaviors and interfaces. Many built-in classes and tools support this type system, which makes them pretty flexible and decoupled.

Duck typing is a core concept in Python. Learning about the topic will help you understand how the language works and, more importantly, how to use this approach in your own code.

In this video course, you’ll learn:

  • What duck typing is and what its pros and cons are
  • How Python’s classes and tools take advantage of duck typing
  • How special methods and protocols support duck typing
  • What alternatives to duck typing you’ll have in Python
Download

Course Slides (.pdf)

5.6 MB
Download

Sample Code (.zip)

4.2 KB

00:00 Welcome to the video course, Getting to Know Duck Typing in Python. I’m Negar from Real Python, and I’ll be your guide through your journey befriending duck typing and learn how you can use it in your code.

00:13 So, what is duck typing?

00:16 Duck typing is like saying if it walks and quacks like a duck, then it’s a duck. In coding, that means you don’t check an object’s type. If it has the right behavior, you just use it.

00:28 In other words, you focus on behavior, not type.

00:33 Let’s understand what all of this means with an example. You’re in charge of making coffee for your office. All you need to do is get coffee from everyone’s machine and pour it into a communal coffee pot.

00:47 Seems straightforward, right?

00:50 Well, that’s until you realize that everyone in your office has their own weird way of making coffee. Dan has a fancy, smart coffee maker, Felix, insists on making French press coffee, and your colleague Alice uses an espresso machine.

01:06 Okay, how do you code this? You go with your gut, you create a base class named Coffee Maker and then inherit from it when you’re defining all the other subclasses for each coffee machine.

01:16 Amazing. Now you have three coffee makers, SmartCoffeeMaker, FrenchPress, and EspressoMachine. They all inherit from CoffeeMaker and have a make_coffee() method.

01:26 Let’s go and test it. You’re testing out two of the classes, EspressoMachine and FrenchPress. You are instantiating them and then storing them in a list called coffee_makers, and then you want to check if they actually make coffee or not.

01:42 So you’re running a for loop and using the make_coffee() method on them, and it works. You get “Espresso shot is ready” and “French coffee is done.” Great. But what if a new colleague brings in a new fancy coffee machine?

01:59 What if 10 new people join at the same time? Do you have to create subclasses for all of these new machines?

02:07 As a matter of fact, you do have a new colleague that has, well, we don’t exactly judge, but an alien coffee orb. He hands you a glowing orb that materializes liquid when you think about caffeine.

02:21 And you’re like, well, that doesn’t exactly fit into my CoffeeMaker class. So what do I do? Then it hits you. At the end of the day, this alien coffee orb is making coffee after all.

02:34 So it does have a make_coffee() method. Even though this isn’t a coffee maker from Earth, so it’s not a CoffeeMaker subclass. It still does behave like one. It makes coffee.

02:49 This exact concept is duck typing. You’re focusing on behavior, not type.

02:57 So you instantiate the AlienCoffeeOrb and use the make_coffee() method on it, and guess what? It works. Alien coffee materialized. Your code works with anything that has the make_coffee() method.

03:12 This means that as long as your objects have the correct method, you can treat all of them the same way. There’s more to the story, of course. There are several ways to implement duck typing and loads of other things to consider, which is what this course is all about.

03:28 Before jumping into the lessons, make sure you’re ready by going through these awesome tutorials and video courses: Object-Oriented Programming in Python, Python Classes, The Power of Object-Oriented Programming, and Python Type Checking.

03:43 These courses and tutorials will give you an amazing foundation to fully take advantage of this video course.

03:51 In this video course, you’ll understand what duck typing is and why it’s useful, you’ll learn how to leverage duck typing in Python’s built-in types. You’ll also see where not to use duck typing, and we’ll explore a few alternatives to it.

04:07 You’ll try combining duck typing and type hints, and you’ll run into some challenges, which you’ll overcome with ABCs.

04:18 Next up is exploring duck typing.

Become a Member to join the conversation.