Duck typing
Утиная типизация
With duck typing, code uses an object if it supports the operations the code needs, regardless of its class. A function that calls `read()`, for example, can work with both an open file and an in-memory text buffer.
What makes an object a good fit?
Say you're writing a function that counts lines. Does it need a file, or does it just need something it can read text from? If reading is all it does, requiring a particular file class would rule out useful alternatives for no benefit. Duck typing takes its name from the saying that if something walks and quacks like a duck, you can treat it as one. In code, the relevant behavior is whatever the caller actually relies on.
How it works at runtime
In Python, you can call source.read() without first checking the object's class. Python looks up read when that line runs. If the object doesn't provide it, you normally get an AttributeError. If read exists but isn't callable or requires an argument you didn't pass, you'll get a TypeError. There's no up-front check of everything the function might do with the object, so an incompatible object can go unnoticed until the relevant code path runs.
Example: counting lines in Python
from io import StringIO
def count_lines(source):
text = source.read()
return len(text.splitlines())
print(count_lines(StringIO('one\ntwo'))) # 2
with open('notes.txt', encoding='utf-8') as source:
print(count_lines(source))
The buffer stores text in memory; the open file reads it from disk. The function doesn't need to know the difference. Its contract is more specific than “has a read method,” though: read() must take no arguments in this call and return text from the current position. Checking hasattr(source, 'read') wouldn't tell you whether either requirement is met.
Where it helps and where it falls short
Duck typing is common with file-like objects, iterables, and test doubles. Here, the in-memory buffer lets you exercise the same line-counting code in a unit test without creating a file. But matching method names and signatures can't guarantee matching behavior. For instance, a read() method that returns bytes might not raise an exception here: bytes have a splitlines() method too, but their line-splitting rules aren't the same as those for text.
Dynamic typing and duck typing answer different questions. Dynamic typing is about when operations are checked; duck typing is about what makes an object usable in a given role. If you want a static check for this example, Python's typing.Protocol can describe a read() method that returns str. A type checker can then check that an object's type matches that interface without requiring it to inherit from the protocol. That's structural typing, and it still doesn't prove what the method actually does.
How it comes up in interviews
You might get a function and be asked which objects it accepts, or be asked to refactor away an unnecessary isinstance check. Start by naming the operations the function really uses, including any assumptions about arguments and results. Then explain which mismatches a type checker could catch and which would need a runtime test.