Skip to Content
Course content

122: Duck Typing Explained

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're at a music venue and you're the sound engineer. You've got a massive amplifier ready to go. You don't actually care if the musician hands you a Fender Stratocaster, a Gibson Les Paul, or some weird boutique synth from Japan. As long as the instrument has a standard 1/4-inch jack that plugs into your cable, you can amplify the sound. You don't stop the show to ask for the instrument's birth certificate or a certified manual proving it's a "Guitar Type Object." If it plugs in and produces a signal, it's a valid instrument for your amp.

That is exactly how Duck Typing works in Python. In many other languages, you have to explicitly state that a function accepts a specific class or an interface. Python is much more relaxed. It doesn't care what an object is; it only cares what the object can do.

Stop Checking IDs at the Door

In a strictly typed language, you might write a function that says, "I only accept objects of the class Dog." But in Python, we just say, "I don't care who you are, as long as you have a speak() method."

class Duck:
    def speak(self):
        print("Quack!")

class Dog:
    def speak(self):
        print("Woof!")

class Robot:
    def speak(self):
        print("Beep boop!")

def make_it_talk(thing):
    # I'm not checking if 'thing' is a Duck or a Dog.
    # I'm just assuming it has a speak() method.
    thing.speak()

# All of these work, even though the types are totally different
make_it_talk(Duck())
make_it_talk(Dog())
make_it_talk(Robot())

Notice that make_it_talk is blissfully ignorant of the classes it's receiving. I didn't use isinstance(thing, Duck). I didn't have to define a parent Animal class. I just called .speak() and let Python handle it. If the object has the method, it works. If it doesn't, Python will throw an AttributeError.

Why This Actually Matters in the Real World

You might be wondering, "Why not just use inheritance and make everything a subclass of Speaker?" You could, but Duck Typing gives you incredible flexibility. It allows you to swap out components without having to rewrite your entire class hierarchy.

Think about file-like objects. In Python, many functions expect an object that has a .read() method. It could be a real file on your hard drive, or it could be a StringIO object (a string acting like a file in memory). Because Python uses duck typing, the function doesn't need to know if it's talking to the disk or the RAM—it just calls .read() and moves on with its life. It makes your code far more generic and reusable.

The Trade-off: Trust but Verify

Now, there is a catch. Since we aren't checking types upfront, we're essentially trusting the caller to pass in something that works. If some brave soul passes an integer into make_it_talk, the program will crash because integers don't have a .speak() method.

I usually handle this in one of two ways. Either I let it crash (because passing an integer where a "speaker" is expected is a bug that should be fixed) or I use a try/except block if I expect that some objects might not be compatible. Avoid the urge to clutter your code with if isinstance(...) checks—that's basically fighting against the way Python is designed to work.




📋 Practical Task

Build a Universal Payment Processor

You are building a checkout system. Your system should be able to process payments regardless of the method used (Credit Card, PayPal, or Crypto). Instead of checking the type of the payment method, use Duck Typing to handle the transaction.

  • Create three classes: CreditCard, PayPal, and Bitcoin.
  • Each class must have its own internal logic, but all three must implement a method called pay(amount).
  • Write a function called process_transaction(payment_method, amount) that takes any object and calls its pay method.
  • Test your function by passing in instances of all three classes to ensure the process_transaction function doesn't care about the specific class, only that the pay method exists.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.