Duck typing uses an object's available operations as the practical interface required by a piece of code, rather than demanding membership in one particular class. The Python glossary describes calling the needed method or using the needed attribute directly. This supports flexible reuse across unrelated implementations.[ref]
For example, a procedure calling source.read() can accept a disk-backed file object or an in-memory object providing a compatible read method. Sharing that method name is insufficient if one method returns text while the procedure requires bytes, or if their error behavior differs. The required interface includes semantics as well as names. Duck typing is commonly used with dynamic typing, but it does not imply that missing or incompatible operations become valid.

Articles by others on the same topic (0)

There are currently no matching articles.