Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Core language & syntaxJuniorVery commonTheory

What does dynamic typing mean in Python, and how does duck typing relate to it?

Answer

Dynamic typing means a variable is just a name bound to an object, and the type lives on the object, not the name, so the same name can be rebound to values of different types at runtime. Duck typing follows from this: code that calls .read() on an argument works with any object that implements .read(), regardless of its class, because Python checks capability through attribute and method lookup rather than declared type.

Strong answer includes

  • type belongs to the object, not the name
  • rebinding a name to a different type
  • capability over declared type

Practical example

A function that calls

Python example
item.upper()

works on a str, and would also work on any custom object that defines an

Python example
upper()

method — no shared base class is required.

#typing#duck-typing

Sources

The Python Language ReferencePython Software Foundation · Precise scoping, evaluation order and expression semanticsPEP 20 — The Zen of PythonPython Software Foundation · Design philosophy behind idiomatic Python choices