{"id":107,"date":"2008-05-27T09:22:42","date_gmt":"2008-05-27T15:22:42","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/2008\/05\/27\/meta-methods-python-vs-tinypy\/"},"modified":"2008-05-27T09:23:21","modified_gmt":"2008-05-27T15:23:21","slug":"meta-methods-python-vs-tinypy","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2008\/05\/27\/meta-methods-python-vs-tinypy\/","title":{"rendered":"meta-methods: python vs tinypy"},"content":{"rendered":"<p>Man, could I have even thought of a title that sounds dustier?  It just screams &#8220;Get ready for the boring lecture of the year folks.&#8221;  I should totally re-name this post to something like &#8220;putting your brain through a blender .. FOR FUN!&#8221;  Or maybe I should have gone the pretentious route and named it something like &#8220;Analysis of modern programming meta-method paradigms.&#8221;  Actually, that sounds even dustier than my original title.  At least my original one falsely implies that there is some competition going on in this post by my use of the &#8220;vs&#8221;.<\/p>\n<p>Here&#8217;s how python does it:<\/p>\n<p>class X:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;def __get__(self,k): return &#8220;OK&#8221;<\/p>\n<p>Here&#8217;s how tinypy does it:<\/p>\n<p>class MetaX:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;def __get__(self,k): return &#8220;OK&#8221;<br \/>\nclass X:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self): setmeta(self,MetaX)<\/p>\n<p>Okay .. so .. the python way is WAY more readable.  Nice \ud83d\ude42  So let me remember if there was a good reason for doing it a different way in tinypy &#8230; I think the answer is &#8220;maybe&#8221; ?  Here&#8217;s what I can come up with:<\/p>\n<ul>\n<li>In tinypy dict == object, which means a.x == a[&#8216;x&#8217;].  <em>(I&#8217;m not sure how that&#8217;s relevant, but it does mean that I have no need for a __getattr__.)<\/em><\/li>\n<li>lua does it that way!  <em>(I have copied many other lua design decisions, so why not copy another!)<\/em><\/li>\n<li>It&#8217;s faster?  <em>(In a sort of, if only tinypy were as fast as python is to begin with kind of way.)<\/em><\/li>\n<li>It was easier to hack together in an afternoon!  <em>(Well, actually .. looking at the python code, I think the python way might have been easier.)<br \/>\n<\/em><\/li>\n<\/ul>\n<p>So what did I gain by going the lua route instead of the python route?  I&#8217;m not entirely sure .. but it sure seemed like a good idea at the time.<\/p>\n<p><strong>A few days later &#8230;<\/strong><\/p>\n<p>With a bit of effort, I&#8217;ve come up with a few more reasons why I did this in tinypy:<\/p>\n<ul>\n<li>Not only are dicts objects but they can also be classes.  This is done by having a __call__ meta, so if you have class X: .. X() calls getmeta(X).__call__.  This is relevant because if the class itself had a __call__ method, how would you define your class so that it knows to create your object with a __call__ method?  <em>(Probably by having a __new__ method that isn&#8217;t copied to the object.)<\/em><\/li>\n<li>The one way the previous point is relevant, is that the object __new__ would probably have to be smarter than how I create objects currently.  It would have to realize that __new__ methods don&#8217;t get copied into objects.<\/li>\n<li>Also, I would probably have to internally track what is a class vs what is an object.  So I know when I do something like X.test(self) .. and my class X has the unbound method of __get__, it knows not to try any meta magic.<\/li>\n<\/ul>\n<p>Okay .. those aren&#8217;t great reasons.  The nice thing about tinypy being .. tinypy .. is that if this continues to bother me for the rest of the day, I can probably try out the implementation in a couple hours.  If you want to see the current meta implementation, it&#8217;s in <a href=\"http:\/\/code.google.com\/p\/tinypy\/source\/checkout\">subversion<\/a>.<\/p>\n<p>Anyone care to comment on the whole lua vs python style meta-methods stuff?  I&#8217;m pretty sure I&#8217;m not sure at all what I think about them!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Man, could I have even thought of a title that sounds dustier? It just screams &#8220;Get ready for the boring lecture of the year folks.&#8221; I should totally re-name this post to something like &#8220;putting your brain through a blender .. FOR FUN!&#8221; Or maybe I should have gone the pretentious route and named it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,32],"tags":[54,55],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-python","category-tinypy","tag-lua","tag-meta-methods"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}