{"id":163,"date":"2007-07-23T23:06:27","date_gmt":"2007-07-24T06:06:27","guid":{"rendered":"https:\/\/dubinko.info\/blog\/2007\/07\/23\/prototypical-inheritance-in-python\/"},"modified":"2007-07-23T23:06:27","modified_gmt":"2007-07-24T06:06:27","slug":"prototypical-inheritance-in-python","status":"publish","type":"post","link":"https:\/\/dubinko.info\/blog\/2007\/07\/prototypical-inheritance-in-python\/","title":{"rendered":"Prototypical inheritance in Python"},"content":{"rendered":"<p>Based on Doug Crockford&#8217;s chapter in <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596510047\/dubinkoinfo-20\">Beautiful Code<\/a>, I wanted to take a crack at implementing <a href=\"http:\/\/javascript.crockford.com\/tdop\/tdop.html\">Top Down Operator Precedence<\/a> in Python. After all, Python and JavaScript are quite similar, right?<\/p>\n<p>Not really. As you can imagine, Doug&#8217;s code makes great use of JavaScript&#8217;s strengths, in this case the ability to assign new methods to any object. For an initial version, I wanted to make the Python version behave the same way, as opposed to a deeper redesign that would be more pythonic. (That would come later.)<br \/>\nMy initial approach was a <code>__getattr__<\/code> method that consisted simply of <code>return getattr(self.prototype, name)<\/code>. When reattaching a new method to an instance, I needed an extra wrapper, done through a <code>wrap<\/code> method which consisted of <code>return new.instancemethod(method, self, self.__class__)<\/code>. It would be used like this: <code>obj.method = obj.wrap(some_func)<\/code>.<\/p>\n<p>This caused a subtle problem that took me a while to track down. In JavaScript, any function can reference the built-in <code>this<\/code> variable, which works whether the function is bound to some specific object or not. (Even global functions are bound to the global object.) But Python doesn&#8217;t have such a keyword. The language prefers the explicit, and uses a explicitly passed parameter, called by convention <code>self<\/code>. The call to wrap a specific function also had the effect of binding the <code>self<\/code> parameter to that particular object; even if it later became a prototype for some other object. This manifested itself as all kinds of broken behavior. For example, the original code has a global <code>scope<\/code> object, and every time a new scope was entered, the global pointed to a newer object that kept a reference to the rest of the scope chain. But in the object&#8217;s methods, <code>self<\/code> pointed to something different than the global. Messy.<\/p>\n<p>Before I get into solutions, I&#8217;d like to see what readers say. How would you go about implementing prototypical inheritance in Python? And what is a more pythonic way to accomplish the same thing? Comment below. Thanks! -m<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Based on Doug Crockford&#8217;s chapter in Beautiful Code, I wanted to take a crack at implementing Top Down Operator Precedence in Python. After all, Python and JavaScript are quite similar, right? Not really. As you can imagine, Doug&#8217;s code makes great use of JavaScript&#8217;s strengths, in this case the ability to assign new methods to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[24,22,1],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-python","category-software","category-stuff"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8eo8l-2D","_links":{"self":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":0,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"wp:attachment":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}