Type Hinting vs Type Checking and Preconditions
jparlar at cogeco.ca
Wed Mar 8 04:10:29 CET 2006
On Mar 7, 2006, at 1:32 PM, Tom Bradford wrote:
> Let me first say that I'm sure that this subject has come up before,
> and so forgive me for beating a dead horse. Secondly, let me say that
> Python's strength is its dynamic nature, and I don't believe that it
> should ever require a precondition scaffolding. With that said, I do
> believe that something like type hinting would be beneficial to the
> Python community, both for tool enablement and for disambiguous
You may want to look at the various "adapt" ideas that have come along
(prompted by Alex Martelli's http://www.python.org/peps/pep-0246.html).
The two dominant implementations of this idea are PJE's PyProtocols,
and Zope.Interfaces. I've used PyProtocols with great success in the
The basic problem with type "hinting" is that it totally breaks duck
typing, which is one of the reasons Python's dynamic nature is so
Just because a function expects a "list object", I should not be
prevented from passing in my own custom object that supports the
necessary parts of the list protocol. This is where adaption makes its
mark, as it is more about supported interfaces on an object than about
It's also important to note that while Guido did spend a lot of time
thinking about optional type markups (and this caused a LOT of hand
wringing in the Python community, the general consensus in the end was
that there was no real benefit from it. (I got the impression that a
lot of the push was from Java/C++ people that didn't really understand
what dynamic languages are about, and wanted to make Python more like
Java/C++. Just my impression though).
And in case you're thinking that maybe we could get some performance
gains out of adding optional types, you should check out Brett Cannon's
Master's thesis. In it, he actually added some typing markup to Python,
and the end result was almost no gain.
HOWEVER: If you feel this is something you still want to have a go at ,
PLEASE try it, and let the community know what your results were. It's
making me sad that there's a lot of talk these days about how unwilling
the Python community is to hear new ideas.
And if you do decide to go forward with it, you should look at the 2.5
branch of the CPython code, as the new AST is supposed to make it
*much* easier to experiment with changes to the language.
More information about the Python-list