{"id":1872,"date":"2009-01-15T22:53:07","date_gmt":"2009-01-15T22:53:07","guid":{"rendered":"http:\/\/justthesam.com\/2009\/01\/python-thoughts\/"},"modified":"2009-01-15T22:53:07","modified_gmt":"2009-01-15T22:53:07","slug":"python-thoughts","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=1872","title":{"rendered":"Python Thoughts"},"content":{"rendered":"<p>I&#39;ve spent a bit more time learning Python, because I&#39;ve been dabbling with Google App Engine (which I&#39;m going off rapidly) but mainly because it&#39;s an interesting language. What&#39;s particularly intriguing to me is its similarity to Ruby, and hence where it differs in syntax or approach makes for a notable point of comparison. Of course it&#39;s probably more correct to say that Ruby is similar to Python than the other way around. From my still very small exposure to Python&#8230;<\/p>\n<p><\/p>\n<div>Nice things about Python:<\/div>\n<div>\n<ul>\n<li>Less confusion with the way classes work. Or maybe I just haven&#39;t stumbled into Python&#39;s equivalent of meta-classes and class vs instance variables.<\/li>\n<li>@classmethod is a much neater way of stating what&#39;s a class method rather than an instance method, compared to all that self gubbins, or the dreaded &lt;&lt;.<\/li>\n<li>Optional named arguments for functions, allowing more flexibility for optional arguments and greater clarity when calling. [As an aside I like Objective-C&#39;s way of building argument labels into the method signature, but not it&#39;s square brackety syntax: [obj message:foo]. I&#39;d much rather do obj.message(foo) and in fact with properties in Objective-C 2.0 we see more of this style.]<\/li>\n<li>I think I probably prefer explicit return statements rather than the Ruby way of returning the last evaluated thing in any expression.<\/li>\n<li>List comprehensions. To start with it just seems like a syntactic difference &#8211; Python: [x*2 for x in my_list] &#0160;Ruby: my_list.map {|x| x*2}. But Python&#39;s party trick is excluding elements as it goes: [x*2 for x in my_list if x != 3].<\/li>\n<\/ul>\n<p>Nasty things about Python:<\/p><\/div>\n<div>\n<ul>\n<li>Seriously &#8211; indentation to demarcate blocks? Apparently I&#39;ll get used to it.<\/li>\n<li>Double underscores. __init__ is a complete pain to type. Why not just a single underscore at the start or something?<\/li>\n<li>Fiddly module system. Why the need for an __init__.py file in a directory, just to make it a module? Why the need to explicitly define an __all__ method just to be able to import everything in a module? I want to be able to create a &quot;model\/&quot; directory, put all the .py files for my DB classes in there, then import the whole lot from my other classes with ease. When I add a new model class, I shouldn&#39;t have to go and modify the __init__.py file. It seems to be a real pain to split code up into multiple files sensibly in Python. If I&#39;ve missed a trick here &#8211; please somebody show me the light!<\/li>\n<li>The string interpolation is OK I suppose, giving the full power of C style formatting, but most of the time you&#39;re just doing simple interpolation and Ruby&#39;s syntax is far more pleasant and readable. Python: &quot;Hello %s, from %s.&quot; % (person, greeter) &#0160;Ruby: &quot;Hello {person}, from {greeter}.&quot; &#0160;Ruby also has a full on formatting system for the few times when you need it.<\/li>\n<\/ul>\n<p><span>&#0160;<\/span>&#0160;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve spent a bit more time learning Python, because I&#39;ve been dabbling with Google App Engine (which I&#39;m going off rapidly) but mainly because it&#39;s an interesting language. What&#39;s particularly intriguing to me is its similarity to Ruby, and hence where it differs in syntax or approach makes for a notable point of comparison. Of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23,33],"tags":[],"class_list":["post-1872","post","type-post","status-publish","format-standard","hentry","category-programming","category-ruby","category-webtech"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1872"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1872\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}