{"id":121,"date":"2009-03-07T11:28:32","date_gmt":"2009-03-07T10:28:32","guid":{"rendered":"http:\/\/fiber-space.de\/wordpress\/?p=121"},"modified":"2009-03-09T06:23:01","modified_gmt":"2009-03-09T05:23:01","slug":"lonely-python-is-intelligent-design-and-how-to-liberate-from-it","status":"publish","type":"post","link":"http:\/\/fiber-space.de\/wordpress\/2009\/03\/07\/lonely-python-is-intelligent-design-and-how-to-liberate-from-it\/","title":{"rendered":"Lonely Python is Intelligent Design and how to liberate from it"},"content":{"rendered":"<p>Python is doomed. Well, of course it is <em>not yet<\/em> doomed. Python is an evolutionary dead end living lonely on its own fitness plateau. It is clear that Python won&#8217;t have offspring because of significant whitespace aka indentation sensitivity. No children, no future.<\/p>\n<p>You might ask where are the news? There are none and we knew this for ever and a decade but sometimes it is in the news again. Read <a href=\"http:\/\/unlimitednovelty.com\/2009\/03\/indentation-sensitivity-post-mortem.html\">this post<\/a> by the author of a programming language called Reia. He struggles endlessly with indentation delimited blocks and finally abandons them for Ruby style `end` terminators ( not before a popularity poll in the Web 2.0 age and consulting the author of the failed Logix project who is now a Ruby advocate). Finally his language looks like Ruby which demonstrates the evolutionary fitness of Ruby. Python on the other hand is a piece of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Intelligent_design\">Intelligent Design<\/a>. Guido van Rossum  has managed a &#8221; beautiful set of compromises&#8221; when he designed the Python grammar. So it ends up in overspecialization whereas languages like JavaScript are flowing more with the nature. To illustrate this nature you might examine the file <a href=\"http:\/\/code.jquery.com\/nightlies\/jquery-nightly.js\">jQuery.js<\/a> that implements the popular jQuery library. The impatients might immediately scroll to the last line. It&#8217;s sweet.<\/p>\n<p>Some people have argued that Haskell style <a href=\"http:\/\/www.haskell.org\/onlinereport\/lexemes.html#lexemes-layout\">layouts<\/a> may save the soul of significant whitespace. Transplanted to Python this would imply mandatory explicit block delimiters that can also be omitted for the now classical indentation style. If you are not willing to decide just give multiple choice style freedom to anyone.<\/p>\n<p>In the rest of the article I&#8217;m discussing optional explicit delimiters for Python built into the grammar and making the statement\/expression distinction weightless. I promise not to cringe when it becomes &#8220;ugly&#8221;. Not behaving like little girls seems to be an evolutionary dead end among male, adult programmers as well but I take it with stoicism.<\/p>\n<p><em>Grammar Extension<\/em><\/p>\n<pre lang=\"bnf\">atom: ( '(' [embedded_stmt|yield_expr] ')' |'[' [listmaker] ']' |\r\n        '{' [dictmaker] '}' |\r\n         '`' testlist1 '`' |\r\n         NAME |  NUMBER | STRING+)\r\ntrailer: '(' [arglist|embedded_suite] ')' | '[' subscriptlist ']' | '.' NAME\r\n\r\nembedded_stmt: ( embedded_funcdef |\r\n    embedded_if_stmt |\r\n    embedded_while_stmt |\r\n    embedded_for_stmt |\r\n    embedded_try_stmt |\r\n    embedded_with_stmt |\r\n    embedded_classdef |\r\n    embedded_small )\r\n\r\nembedded_small: small_stmt (';' small_stmt)*\r\nembedded_suite: embedded_stmt+\r\nembedded_funcdef: [decorators] 'def' NAME parameters ':' embedded_suite\r\nembedded_if_stmt: ( 'if' test ':' embedded_suite ('elif' test ':' embedded_suite)*\r\n                   ['else' ':' embedded_suite])\r\nembedded_while_stmt: 'while' test ':' embedded_suite ['else' ':' embedded_suite]\r\nembedded_for_stmt: ( 'for' exprlist 'in' testlist ':' embedded_suite\r\n                     ['else' ':' embedded_suite] )\r\nembedded_try_stmt: ('try' ':' suite ((except_clause ':' embedded_suite)+\r\n                    ['else' ':' embedded_suite] ['finally' ':' embedded_suite] |\r\n                   'finally' ':' embedded_suite))\r\nembedded_with_stmt: 'with' test [ with_var ] ':' embedded_suite\r\nembedded_classdef: 'class' NAME ['(' [testlist] ')'] ':' embedded_suite<\/pre>\n<p>This is a conservative grammar extension of the official <a href=\"http:\/\/www.fiber-space.de\/misc\/Grammar_2_5\">Python 2.5 Grammar<\/a>. Conservative means that all expressions \/ statements written in Python 2.5 remain valid. Those  non-terminals that substitute existing ones are `atom` and `trailer`. In detail the `testlist_gexp` non-terminal in `atom`  is replaced by the newly defined `embedded_stmt` non-terminal and instead of an `arglist` an `embedded_suite`can be used  . The `embedded_stmt` non-terminal is the gateway for all kinds of statements embedded into expressions. The statement-expression distinction becomes de facto obsolete.<br \/>\nWhat happens to significant whitespace? Post-processing the token stream filters whitespace inside of parentheses. Hence the token stream won&#8217;t contain `NEWLINE`, `INDENT`and `DEDENT` token within expressions. All those `embedded_xxx` non-terminals are only adjustments of existing ones reflecting the absence of the mentioned whitespace token in expressions. Notice that you can&#8217;t mix. Once you are living in a grammatical expression whitespace becomes definitely insignificant.<\/p>\n<p>The grammar is not LL(1) and you need a stronger parser than Pythons own one to handle it.  There is also no behavior associated with the new definitions. Semantics shall be entirely neglected and only the future shape of things shall be highlighted. Here are a few examples:<\/p>\n<p><strong>Multiline Lambda<\/strong><\/p>\n<pre lang=\"python\">lambda x,y: (\r\n    z = x+y;              # semicolons always separate small statements\r\n    print z;\r\n    return z\r\n)(3, 2)<\/pre>\n<p><strong>Long Assertions<\/strong><\/p>\n<pre lang=\"python\">assert ( if x&gt;0:\r\n    x == 2\r\n    else:\r\n        x == -2)<\/pre>\n<p><strong>The Return of the Return<\/strong><\/p>\n<pre lang=\"python\">def foo(x):\r\n    return ( return )<\/pre>\n<p><strong>My Property<br \/>\n<\/strong><\/p>\n<pre lang=\"python\">x = property(\r\n    def fget(self):\r\n        return self._x\r\n    def fset(self, value):\r\n        self._x = value\r\n)<\/pre>\n<p><strong>Function in the middle<br \/>\n<\/strong><\/p>\n<pre lang=\"python\">x + (def fact(n):\r\n     if n&lt;=1:\r\n          return 1\r\n     else:\r\n          return fact(n-1))(7) + z<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python is doomed. Well, of course it is not yet doomed. Python is an evolutionary dead end living lonely on its own fitness plateau. It is clear that Python won&#8217;t have offspring because of significant whitespace aka indentation sensitivity. No &hellip; <a href=\"http:\/\/fiber-space.de\/wordpress\/2009\/03\/07\/lonely-python-is-intelligent-design-and-how-to-liberate-from-it\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,12,4],"tags":[],"_links":{"self":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/121"}],"collection":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":27,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":168,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/121\/revisions\/168"}],"wp:attachment":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}