PyContracts: declare constraints on function parameters and return values (2014) [pdf]
I&#x27;ll offer this qualifier up front: Python is a great language with many interesting features and it has a fantastic spot in the world of data analysis. Having said that, I wonder if contract programming is appropriate in Python? I have a similar criticism of contract programming in Ruby and PHP and Perl.<p>I&#x27;m a huge fan of &quot;design by contract&quot; and I strongly advocate for that style when I&#x27;m working in any JVM language (Java, Groovy, Clojure, etc). But if you find yourself needing this kind of run-time validation in Python, shouldn&#x27;t you stop and ask yourself &quot;Is Python the right tool for this job?&quot; (If anyone here remembers reading some old posts of mine, you might recall that last year I was involved in a startup focused on Natural Language Processing, which we first considered using Python for, but then switched to Java.)<p>My criticism is stronger for Ruby and PHP. I realize that some people compile their Python, which allows optimizations similar to what&#x27;s available on the JVM. My advice is less absolute when it comes to Python. Whereas with PHP, if you find yourself needing runtime contracts, I would always, always suggest you switch to a different language, I realize the situation is somewhat different with Python. There are more compile and run time options.<p>The great advantage of the scripting languages is their ease-of-getting-started. PHP and Ruby and Python all recommend themselves to beginners because of the simplicity of the initial setup.<p>I am willing to believe that Python scales up better than PHP or Ruby. I&#x27;ve seen enough disasters with Symfony and Rails that I&#x27;m willing to give Python some credit here (though consider how many complete re-writes Zope undertook).<p>For my part, as soon as the run time is complicated enough that I feel I need contracts for the runtime environment, I would consider switching to some other language.