<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Ruby Style Adding Methods to Existing Classes in Python?</title>
	<atom:link href="http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/</link>
	<description>Parand Tony Darugar: A Cruel and Petty Dictator</description>
	<lastBuildDate>Thu, 10 May 2012 16:52:02 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Extending plugins in PHP &#124; Code Penguin</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-230242</link>
		<dc:creator>Extending plugins in PHP &#124; Code Penguin</dc:creator>
		<pubDate>Wed, 25 Mar 2009 00:43:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-230242</guid>
		<description>[...] some languages you can alter classes dynamically, and it can be referred as monkeypatching. While it is actually possible in PHP, it is ugly at best; you end up typing code in character [...]</description>
		<content:encoded><![CDATA[<p>[...] some languages you can alter classes dynamically, and it can be referred as monkeypatching. While it is actually possible in PHP, it is ugly at best; you end up typing code in character [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Bicking</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10214</link>
		<dc:creator>Ian Bicking</dc:creator>
		<pubDate>Tue, 18 Jul 2006 19:35:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10214</guid>
		<description>A possibly handy little decorator for injection methods that I wrote is magic_set at the bottom of this file: http://svn.colorstudy.com/home/ianb/ruby_blocks.py</description>
		<content:encoded><![CDATA[<p>A possibly handy little decorator for injection methods that I wrote is magic_set at the bottom of this file: <a href="http://svn.colorstudy.com/home/ianb/ruby_blocks.py" rel="nofollow">http://svn.colorstudy.com/home/ianb/ruby_blocks.py</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Lemire</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10209</link>
		<dc:creator>Daniel Lemire</dc:creator>
		<pubDate>Tue, 18 Jul 2006 18:24:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10209</guid>
		<description>The following blog post I wrote quite some time ago relates to your question:

http://www.daniel-lemire.com/blog/archives/2005/12/21/metaclass-programming-in-python/</description>
		<content:encoded><![CDATA[<p>The following blog post I wrote quite some time ago relates to your question:</p>
<p><a href="http://www.daniel-lemire.com/blog/archives/2005/12/21/metaclass-programming-in-python/" rel="nofollow">http://www.daniel-lemire.com/blog/archives/2005/12/21/metaclass-programming-in-python/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Parand Darugar</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10205</link>
		<dc:creator>Parand Darugar</dc:creator>
		<pubDate>Tue, 18 Jul 2006 16:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10205</guid>
		<description>Harry, much thanks, your example is very helpful. It *is* quite easy to add methods to existing classes in Python. The formatting was lost on your post, so I&#039;ve uploaded the file &lt;a rel=&quot;nofollow&quot; href=&quot;http://www.parand.com/say/misc/PythonMethodInjection.py&quot;&gt;here&lt;/a&gt; (since formatting matters so much in this example).

Regarding the Javascript example, wow, 60,000 lines of Javascript. I wouldn&#039;t wish that on my worst enemy.

&quot;I find myself wishing for build-time tools, a compiler to catch typos in
variable names, to catch syntax errors earlier rather than runtime
checking.&quot;

This argument can be applied to any scripting language. I think Javascript is a particularly horrible language and I&#039;d tend to lay the blame there as opposed to compile time rather than runtime enforcement of syntax, etc.</description>
		<content:encoded><![CDATA[<p>Harry, much thanks, your example is very helpful. It *is* quite easy to add methods to existing classes in Python. The formatting was lost on your post, so I&#8217;ve uploaded the file <a rel="nofollow" href="http://www.parand.com/say/misc/PythonMethodInjection.py">here</a> (since formatting matters so much in this example).</p>
<p>Regarding the Javascript example, wow, 60,000 lines of Javascript. I wouldn&#8217;t wish that on my worst enemy.</p>
<p>&#8220;I find myself wishing for build-time tools, a compiler to catch typos in<br />
variable names, to catch syntax errors earlier rather than runtime<br />
checking.&#8221;</p>
<p>This argument can be applied to any scripting language. I think Javascript is a particularly horrible language and I&#8217;d tend to lay the blame there as opposed to compile time rather than runtime enforcement of syntax, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry Fuecks</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10179</link>
		<dc:creator>Harry Fuecks</dc:creator>
		<pubDate>Tue, 18 Jul 2006 09:35:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10179</guid>
		<description>Sorry - further point on that link http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1 - it&#039;s the section &quot;8. JavaScript is too flexible&quot; I was referring to</description>
		<content:encoded><![CDATA[<p>Sorry &#8211; further point on that link <a href="http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1" rel="nofollow">http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1</a> &#8211; it&#8217;s the section &#8220;8. JavaScript is too flexible&#8221; I was referring to</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry Fuecks</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10178</link>
		<dc:creator>Harry Fuecks</dc:creator>
		<pubDate>Tue, 18 Jul 2006 09:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10178</guid>
		<description>Interested to know the ins and outs here as well.

Think it needs careful definition of different things you can do - adding a method to a class at runtime is not the same as adding a method to an object (an instance of the class). That &quot;new&quot; approach seems to address the latter.

The former is fairly easy e.g.


class Foo:
    
    def __init__(self):
        self.x = &quot;x = 1&quot;
        self.y = &quot;y = 2&quot;
    
    def showx(self):
        print self.x

def showy(self):
    print self.y

if __name__ == &#039;__main__&#039;:
    
    # Attach at runtime...
    Foo.showy = showy
    
    f2 = Foo()
    f2.showx()
    f2.showy()


Now all instances of Foo, created _after_ attaching showy, have this method available. The downside there is the definition of showy has &quot;self&quot; embedded - it can&#039;t also just be a function but perhaps that&#039;s a rare requirement.

&quot;Why am I losing faith in inheritence?&quot; the desire to avoid rigid code, gain more flexibility and (perhaps) save time?

&quot;Is this idea of adding methods to existing classes/instances actually a bad idea?&quot; I guess three possible issues (but not show stoppers) might be;

 - it&#039;s potentially expensive performance wise - if you&#039;re doing this a lot, you might have missed the chance save some cycles by hardcoding, allowing python to compile upfront

 - it&#039;s harder to analyse the source code automatically and understand it&#039;s semantics, without actually executing it

 - it&#039;s (subjective) easier for teams of developers to working with concrete / static class definitions. I find this discussion relevant: http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1 - although that&#039;s Javascript, think it applies

&quot;Is injection of methods into a class equivalent to inheriting from it and extending it?&quot; - does the question matter? Think inheritance is Python is more of a notionally supported feature than something rock solid, as in Java. You have multiple inheritance. You have explicit self rather than implicit &quot;this&quot;. It&#039;s more like - what&#039;s the easiest way to get re-use?</description>
		<content:encoded><![CDATA[<p>Interested to know the ins and outs here as well.</p>
<p>Think it needs careful definition of different things you can do &#8211; adding a method to a class at runtime is not the same as adding a method to an object (an instance of the class). That &#8220;new&#8221; approach seems to address the latter.</p>
<p>The former is fairly easy e.g.</p>
<p>class Foo:</p>
<p>    def __init__(self):<br />
        self.x = &#8220;x = 1&#8243;<br />
        self.y = &#8220;y = 2&#8243;</p>
<p>    def showx(self):<br />
        print self.x</p>
<p>def showy(self):<br />
    print self.y</p>
<p>if __name__ == &#8216;__main__&#8217;:</p>
<p>    # Attach at runtime&#8230;<br />
    Foo.showy = showy</p>
<p>    f2 = Foo()<br />
    f2.showx()<br />
    f2.showy()</p>
<p>Now all instances of Foo, created _after_ attaching showy, have this method available. The downside there is the definition of showy has &#8220;self&#8221; embedded &#8211; it can&#8217;t also just be a function but perhaps that&#8217;s a rare requirement.</p>
<p>&#8220;Why am I losing faith in inheritence?&#8221; the desire to avoid rigid code, gain more flexibility and (perhaps) save time?</p>
<p>&#8220;Is this idea of adding methods to existing classes/instances actually a bad idea?&#8221; I guess three possible issues (but not show stoppers) might be;</p>
<p> &#8211; it&#8217;s potentially expensive performance wise &#8211; if you&#8217;re doing this a lot, you might have missed the chance save some cycles by hardcoding, allowing python to compile upfront</p>
<p> &#8211; it&#8217;s harder to analyse the source code automatically and understand it&#8217;s semantics, without actually executing it</p>
<p> &#8211; it&#8217;s (subjective) easier for teams of developers to working with concrete / static class definitions. I find this discussion relevant: <a href="http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1" rel="nofollow">http://groups.yahoo.com/group/wdf-dom/message/4136&amp;threaded=1</a> &#8211; although that&#8217;s Javascript, think it applies</p>
<p>&#8220;Is injection of methods into a class equivalent to inheriting from it and extending it?&#8221; &#8211; does the question matter? Think inheritance is Python is more of a notionally supported feature than something rock solid, as in Java. You have multiple inheritance. You have explicit self rather than implicit &#8220;this&#8221;. It&#8217;s more like &#8211; what&#8217;s the easiest way to get re-use?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Willison</title>
		<link>http://parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/comment-page-1/#comment-10172</link>
		<dc:creator>Simon Willison</dc:creator>
		<pubDate>Tue, 18 Jul 2006 08:06:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.parand.com/say/index.php/2006/07/17/ruby-style-adding-methods-to-existing-classes-in-python/#comment-10172</guid>
		<description>I haven&#039;t played with this kind of thing myself, but it looks like the comments attached to that cookbook entry (in particular Alex Martelli&#039;s usage of the &#039;new&#039; module) describe the &quot;correct&quot; approach. Ruby certainly makes it easier to do this though - it&#039;s a common idiom in Ruby, but quite rare in Python.</description>
		<content:encoded><![CDATA[<p>I haven&#8217;t played with this kind of thing myself, but it looks like the comments attached to that cookbook entry (in particular Alex Martelli&#8217;s usage of the &#8216;new&#8217; module) describe the &#8220;correct&#8221; approach. Ruby certainly makes it easier to do this though &#8211; it&#8217;s a common idiom in Ruby, but quite rare in Python.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

