With dynamic typing, the types of values determine the operations permitted during execution, while a variable name can be rebound to values of different types. This differs from changing the type of one existing value. In Python,
x=3; x="three" rebinds the name; the integer object has not become a string.[ref]Python 3.14 Data Model. §3.1, Objects, values and types
A call can succeed for several classes if each supplies the operations the function actually uses. This is often called duck typing: a function iterating over its argument can accept different iterable objects. An unsupported operation still fails when reached. Dynamic typing does not imply absence of a type system, automatic conversion between every pair of types, or that every error waits until execution; syntax errors and some other checks can occur earlier.
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]
Glossary, Python 3.14 documentation. duck-typing entry
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
There are currently no matching articles.