{"id":7032,"date":"2005-05-16T00:00:00","date_gmt":"2005-05-15T14:00:00","guid":{"rendered":"http:\/\/michael.ellerman.id.au\/blog\/2005\/05\/16\/real-work-with-python\/"},"modified":"2007-10-25T13:56:01","modified_gmt":"2007-10-25T03:56:01","slug":"real-work-with-python","status":"publish","type":"post","link":"https:\/\/michael.ellerman.id.au\/blog\/2005\/05\/16\/real-work-with-python\/","title":{"rendered":"&#8220;Real work&#8221; with Python"},"content":{"rendered":"<p>I just stumbled across <a href=\"http:\/\/members.optusnet.com.au\/benjamincarlyle\/benjamin\/blog\/\">Benjamin Carlyle&#8217;s<\/a> piece titled <a href=\"http:\/\/members.optusnet.com.au\/benjamincarlyle\/benjamin\/blog\/2005\/05\/14\/\">Hungarian Notation Revisited<\/a>. He&#8217;s replying to a reply to a blah blah of an article by <a href=\"http:\/\/www.joelonsoftware.com\/articles\/Wrong.html\">Joel<\/a> on (among other things) differentiating &#8220;safe&#8221; and &#8220;unsafe&#8221; strings in web apps.<\/p>\n<p>I absolutely agree with his point that Hungarian notation is broken because it asks the programmer to do the compiler&#8217;s job. I also think it makes code much <strong>harder<\/strong> to read, because you&#8217;re constantly tripping over details that are (usually) irrelevant to the underlying algorithm.<\/p>\n<p>He goes on to suggest that Python isn&#8217;t suitable for &#8220;real work&#8221; because of its &#8220;lack of ability to state the expectations of a particular function&#8221;. His premise <strong>is<\/strong> wrong, and I also believe his conclusion is wrong, but I leave that as matter of <strike>religion<\/strike> opinion.<\/p>\n<p>Strict typing in Python is not the default, but it is painless:<\/p>\n<pre>\r\n<strong>class<\/strong> SafeString<span style=\"color: #ff00ff\">(<\/span><span style=\"color: #800000\">str<\/span><span style=\"color: #ff00ff\">):<\/span>\r\n    <strong>def<\/strong> __init__<span style=\"color: #ff00ff\">(<\/span><span style=\"color: #008000\">self<\/span>, value<span style=\"color: #ff00ff\">):<\/span>\r\n        <span style=\"color: #808080\"><em># check value is 'safe' here, then call super class constructor<\/em><\/span>\r\n        <span style=\"color: #800000\">str<\/span>.__init__<span style=\"color: #ff00ff\">(<\/span><span style=\"color: #008000\">self<\/span>, value<span style=\"color: #ff00ff\">)<\/span>\r\n\r\n<strong>def<\/strong> only_print_safe_string<span style=\"color: #ff00ff\">(<\/span>s<span style=\"color: #ff00ff\">):<\/span>\r\n    <strong>assert<\/strong> <span style=\"color: #800000\">isinstance<\/span><span style=\"color: #ff00ff\">(<\/span>s, SafeString<span style=\"color: #ff00ff\">)<\/span>\r\n    <strong>print<\/strong> s\r\n\r\ns <span style=\"color: #ff00ff\">=<\/span> SafeString<span style=\"color: #ff00ff\">(<\/span><span style=\"color: #ff0000\">'This is a safe string'<\/span><span style=\"color: #ff00ff\">)<\/span>\r\n\r\nonly_print_safe_string<span style=\"color: #ff00ff\">(<\/span>s<span style=\"color: #ff00ff\">)<\/span>\r\nonly_print_safe_string<span style=\"color: #ff00ff\">(<\/span><span style=\"color: #ff0000\">'This will not get printed'<\/span><span style=\"color: #ff00ff\">)<\/span><\/pre>\n<p>Coming from C or C++ that probably seems a bit weird. &#8220;I have to add an assertion just to get type checking?&#8221;<\/p>\n<p>But if you think about it, in a dynamic language, that&#8217;s all type checking is, an assertion at the function entry point. Python gives you the flexibility to do it when you need to, but only then.<\/p>\n<p>I would also argue that strict typing on its own, is rarely enough to do proper <a href=\"http:\/\/en.wikipedia.org\/wiki\/Design_by_contract\">Design by Contract<\/a>. In most cases the prerequisites for a function can not be expressed simply by the types of the arguments, you need to assert something about their <em>values<\/em>. And in those cases, even in C and C++, what you end up with is assert.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just stumbled across Benjamin Carlyle&#8217;s piece titled Hungarian Notation Revisited. He&#8217;s replying to a reply to a blah blah of an article by Joel on (among other things) differentiating &#8220;safe&#8221; and &#8220;unsafe&#8221; strings in web apps. I absolutely agree with his point that Hungarian notation is broken because it asks the programmer to do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[28],"_links":{"self":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/7032"}],"collection":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/comments?post=7032"}],"version-history":[{"count":0,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/posts\/7032\/revisions"}],"wp:attachment":[{"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/media?parent=7032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/categories?post=7032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michael.ellerman.id.au\/blog\/wp-json\/wp\/v2\/tags?post=7032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}