Adaptation, Protocols, and Python 3000

by Jeremy Jones

Python 3000 has been a joke for so long now that it's funny to consider that it will eventually materialize into something real. Guido just posted some thoughts on very real implementation details for Python 3000. The topic at hand was adaptation. Basically, Guido has killed PEPS 245 and 246 which are interfaces and adaptation respectively, and has posited the use of generic functions as an alternative approach.

I'm curious to see how Guido and the other Python big-brains envision everyday use of any kind of protocol-based approach for type management (or whatever you want to term the topic at hand). From one of Alex Martelli's posts, I can see how protocols could be really cool for library, specifically the standard library, developers. You create a library funtion, state that it can handle this protocol of thing, and it (not-so-)magically just does the right thing with various types that are passed in. For example, you can create a function that really expects a file-like object, create a protocol that adapts a string to a StringIO object, and now it can handle a file or a string. At least that's how I understand things so far.

As I've been watching the discussion around protocols, I've assumed that this was going to be Python's way of gently (and optionally) enforcing type "safety" or requiring function/method call arguments to be of certain types, but it appears to be much more than that. I really like the idea. I'd just like to see more day-to-day use cases rather than just the theoretical discussions that I've read thus far. From PEP 246, there is a question "What benefit does this proposal provide?", to which the answer begins "[t]he typical Python programmer is an integrator" and seems to sound like protocols will just "make stuff work" if a sensible type of object is passed into a function or method. So, maybe that's the "common, everyday use case" that I was looking for. Maybe it just makes stuff work easier and integrate better. How protocols and adaptation fit into my original "type safety" thoughts is becoming less clear. I was really hoping Python would incorporate optional type checking at some point. Maybe this protocol approach can provide a foundation for type checking. Or maybe that's an entirely different discussion.