Software Design by Example in Python 9: Protocols
As I wrote yesterday
I didn’t want to spend an entire chapter of Software Design by Example on closures,
but I needed to be sure learners understood them
before presenting mock objects, with
statements, and how for
loops use iterators.
What I try to show in Chapter 9: Protocols
is that these are all examples of a protocol:
if a program has feature X,
then language feature Y will make use of it.
I’m a little embarrassed to admit how long it took me to realize that protocols like this are everywhere in modern languages. Constructors? That’s a protocol. Operator overloading? That’s another, but in my mind they were both things in their own right, not instances of a larger concept. I don’t think we need to go all the way to metaobject protocols when teaching this (at least, not to this audience); if you have thoughts on whether this approach works, I’d welcome feedback.
Terms defined: append mode, context manager, decorator, infinite recursion, iterator, Iterator pattern, mock object, protocol.