Template Compilation and Limitations
A template is written to use certain methods or properties, and an argument is suitable when it provides them.
Two Ways to Judge a Value
Most programs need to decide whether a value is suitable for a particular job. There are two broad ways to make that decision. One way is to ask what kind of thing the value is. The other is to ask what the value can do. Duck typing is the second approach. In this article you will see how that idea applies to template compilation, and why the course treats it as a feature of templates rather than as a general rule for every piece of code.
What do you think happens?
If a piece of code needs a value that can speak, and you have two objects of completely unrelated types, both of which can speak, should one of them be rejected just because the types differ?
Reveal answer
No, if each object provides what the code needs, both can be suitable
Duck typing decides suitability by the methods or properties present. Two objects with completely different actual types can both qualify if each one has what the code requires.
Walks Like a Duck
The name comes from a well-known saying: if something walks like a duck and quacks like a duck, it can be treated as a duck. The saying does not ask what species the animal really is. It looks only at observable behaviour. Duck typing applies the same reasoning to program values. It does not ask what type a value belongs to. It asks whether the value shows the behaviour the code depends on.
Duck typing decides an object's suitability by the presence of certain methods or properties rather than by its actual type.
Presence Matters More Than Type
The important word in the definition is presence. What matters is whether the object has the methods or properties that the code needs. This has two consequences that are easy to miss. First, two objects with completely different actual types can both be suitable, as long as each one has what the code requires. Second, the reverse also holds: an object of an apparently appropriate type is not suitable if the required methods or properties are missing. The type label alone does not settle the question.
A Small Speak Example
The following small example is not taken from the course material. It is written to show the idea of duck typing in a simple setting. The function announce does not check what kind of object it receives. It only requires that the object has a speak method, so any object that provides speak can be passed in.
def announce(thing):
thing.speak()
class Dog:
def speak(self):
return 'Woof'
class Robot:
def speak(self):
return 'Beep'
announce(Dog())
announce(Robot())- 1The function announce accepts one argument named thing and calls its speak method.
- 2Dog defines speak, so a Dog object is suitable for announce.
- 3Robot also defines speak, so a Robot object is suitable too, even though it has no relationship to Dog.
- 4If you passed an object with no speak method, the call would fail because the required member is missing.
- announce never inspects the type of thing
- The only requirement is that thing has a speak method
- Dog and Robot are unrelated classes, yet both pass the same check
What do you think happens?
Suppose you pass to announce an object of a class that has a name property but no speak method. Is that object suitable?
Reveal answer
No, it is not suitable
announce requires a speak method. An object lacking that method is not suitable, regardless of its class or its other properties.
What Compile-Time Means Here
The course material makes one specific statement about this topic: templates offer compile-time duck typing. The qualifier compile-time matters. It indicates that the suitability check associated with duck typing takes place when the code is compiled, rather than while the program runs. The principle is the same as in the speak example. A template is written to use certain methods or properties, and an argument is suitable when it provides them.
Read the diagram as a rule about where the decision is made. The check belongs to compilation, so the question being asked is whether the instantiated template's needs are met by the type supplied. If the required members are present, the argument is suitable. If a required member is missing, the argument does not qualify. The course material presents this as a property of templates specifically, not as a general rule that applies to every piece of code.
Duck Typing Beside Type Checking
A useful way to fix the idea is to set it beside the alternative. Checking the actual type asks whether an object belongs to a particular type. Duck typing asks whether the object has the methods or properties needed for the task. The two questions can give different answers for the same object, because one looks at classification and the other looks at capability.
| Question asked | Checking actual type | Duck typing |
|---|---|---|
| What is examined | The type the object belongs to | The methods or properties the object has |
| Deciding factor | Belonging to a particular type | Presence of the needed members |
| Unrelated types | Not accepted by the type question alone | Can both qualify if each provides what the code needs |
| Object of an apparently right type | Accepted by the type question | Rejected if a required member is missing |
Two questions about the same object
How Members Connect to Suitability
The idea can be seen as a small network of relationships. An object has methods and properties. Those members are what the template or code uses. Their presence decides whether the object is suitable. The object's actual type sits outside this chain. It does not decide suitability under the duck typing rule.
Misreadings to Avoid
Most errors with duck typing come from two places: treating the type name as decisive, or assuming that a missing member is acceptable. The items below describe these errors and the correct reading in each case.
Common mistake Assuming any object works
What it looks likePassing any object to announce because it is a valid object
Why it failsSuitability depends on whether the object provides the required methods or properties, not on the fact that it is an object.
Do this insteadCheck that the object has the speak method before expecting announce to accept it.
Common mistake Treating the type name as decisive
What it looks likeRejecting a Robot object because it is not a Dog, even though both provide speak
Why it failsUnrelated types can both qualify when each provides what the code needs.
Do this insteadAsk whether the object has the needed members rather than whether it has a particular type name.
Common mistake Assuming a missing member is acceptable
What it looks likeExpecting announce to work on an object with a name property but no speak method
Why it failsAn object of an apparently appropriate type is not suitable if the required methods or properties are missing.
Do this insteadTreat a missing required member as a reason the argument is not suitable.
Common mistake Applying the rule to all code
What it looks likeAssuming every function in every program is checked by presence of members at compile time
Why it failsThe course material presents compile-time duck typing as an offering of templates, not as a general rule for every piece of code.
Do this insteadKeep the compile-time presence rule in the context of templates when reasoning about template code.
Testing the Rule on New Objects
A template requires that its argument has a speak method and a volume property. Three objects are offered: object A is of type Dog and has speak and volume; object B is of type Robot and has speak only; object C is of type Speaker and has volume only. For each object, decide whether it is suitable and say which member decides your answer. Then explain why the actual types alone cannot give you the answer.
Hints
- Ask only whether each object has the required members, ignoring its type name.
- An object is suitable only if every required member is present.
- Two objects of unrelated types can both be suitable, and a type name that looks right can still fail.
Key Takeaways
- 1Duck typing decides an object's suitability by the presence of certain methods or properties rather than by its actual type.
- 2The course material states that templates offer compile-time duck typing, so the suitability check is associated with compilation rather than runtime.
- 3Unrelated types can both qualify if each provides what the code needs, and an apparently appropriate type fails if a required member is missing.
- 4Errors come from treating the type name as decisive or from assuming a missing member is acceptable.
At a glance
- Duck typing decides suitability by the methods or properties an object has, not by its actual type.
- Templates offer compile-time duck typing, so the presence check belongs to compilation rather than to program execution.
- Checking the actual type asks about classification, while duck typing asks about capability; the two can disagree.
- Treating the type name as decisive, or assuming a missing member is acceptable, are the typical errors to avoid.