Duck Typing

say it: “DUK TYPE-ing” /ˈdʌk ˈtaɪpɪŋ/

Python cares what an object can do, not what class it is.

What is duck typing?

Duck typing means Python cares what an object can do, not what class it is: "if it walks like a duck and quacks like a duck, it's a duck".

class Printer:
    def write(self, text):
        print(text)

def save_report(output):
    output.write("Monthly sales: $12,400")   # anything with write() works

save_report(Printer())
save_report(open("report.txt", "w"))

Level: Freelancer · More Python terms at this level

Updated