Featurecore
Duck typing
Also called duck-typing, EAFP, LBYL, structural typing at run time, file-like object, protocol by convention.
Duck typing means code uses the methods it needs and does not check the type of the object. Any object with those methods works, and a missing method shows up as an exception when the code runs.
- Language
- Python
Synopsis
class Duck:
def speak(self):
return "Quack"
class Robot:
def speak(self):
return "Beep"
def chorus(things):
return " ".join(t.speak() for t in things)
print(chorus([Duck(), Robot(), Duck()]))
Quack Beep Quack
Duck and Robot have no shared base class. chorus only calls speak, so it accepts both.
Semantics
r1. Duck typing is "A programming style which does not look at an object’s type to determine if it has the right interface; instead, the method or attribute is simply called or used".1 "Duck-typing avoids tests using type() or isinstance()."1
r2. "A piece of Python code that expects a particular abstract data type can often be passed a class that emulates the methods of that data type instead."2 Example: file-like.
r3. Duck typing "typically employs hasattr() tests or EAFP programming."1 EAFP "assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false."1 Example: eafp.
r4. LBYL "explicitly tests for pre-conditions before making calls or lookups."1 "In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”."1
r5. "duck-typing can be complemented with abstract base classes."1
Examples
import io
def count_lines(f):
return sum(1 for _ in f)
class FakeFile:
def __init__(self, text):
self.lines = text.splitlines(keepends=True)
def __iter__(self):
return iter(self.lines)
print(count_lines(io.StringIO("a\nb\nc\n")), count_lines(FakeFile("x\ny\n")))
3 2
count_lines only iterates, so a StringIO or a small class with __iter__ takes the place of a file (rule r2).
def area(shape):
try:
return shape.area()
except AttributeError:
return None
class Sq:
def area(self):
return 4
print(area(Sq()), area("not a shape"))
4 None
The function tries the call and handles the failure (rule r3).
def area(shape):
if hasattr(shape, "area"):
return shape.area()
return None
class Sq:
def area(self):
return 4
print(area(Sq()), area(42))
4 None
The same function in the LBYL style tests first with hasattr (rule r4).
Pitfalls
A type check rejects objects that would work:
This code runs but gives a wrong answer on purpose.
import io
def first_line(f):
if type(f) is not io.TextIOWrapper:
return "not a file"
return f.readline()
print(first_line(io.StringIO("hello\nworld\n")))
not a file
StringIO has readline, but the type test refuses it. Call readline and let a missing method raise (rule r1).
An except AttributeError around the whole call also hides bugs inside the method:
This code runs but gives a wrong answer on purpose.
class Broken:
def area(self):
return self.sise * 2
def area(shape):
try:
return shape.area()
except AttributeError:
return None
print(area(Broken()))
None
The typo sise raises AttributeError inside area, and the handler treats it as a missing method. Get the method first with getattr(shape, 'area', None), and call it outside the try statement.
Comparison
"Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here."3 "A Go type implements an interface by implementing the methods of that interface, nothing more."4 So a Go type, like a Python class, does not name the interface. But a Go function parameter has an interface type, and the FAQ says: "You can ask the compiler to check that the type T implements the interface I by attempting an assignment".4 In Python nothing lists the methods, and a missing one raises an exception when the code runs (rule r1).
References
- Glossary, duck-typing; EAFP; LBYL
- The Python Tutorial, 9. Classes, 9.7. Odds and Ends
- Effective Go, Interfaces
- Frequently Asked Questions (FAQ), The Go Programming Language, Why doesn’t Go have “implements” declarations?; How can I guarantee my type satisfies an interface?
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag YY92BY Edit this page History Report an error Markdown