Can I replace an existing method of an object in Python? : <p><strong>Q:</strong> Is there a way to alter a method of an existing object in Python (3.6)? (By "method" I mean a function that is passed <code>self</code> as an argument.)</p>

<hr>

<p><strong>Example</strong></p>

<p>Let's say I have a class <code>Person</code> having some very useful method <code>SayHi()</code>:</p>

<pre><code>class Person(object):
    Cash = 100
    def HasGoodMood(self):
        return self.Cash &gt; 10

    def SayHi(self):
        if self.HasGoodMood():
            print('Hello!')
        else:
            print('Hmpf.')

&gt;&gt;&gt; joe = Person()
&gt;&gt;&gt; joe.SayHi()
Hello!
</code></pre>

<p>As you can see, the response of the person depends on their current mood computed by the method <code>HasGoodMood()</code>. A default person has good mood whenever they have more than 10$ cash on them.</p>

<p>I can easily create a person who does not care about the money and is happy all the time:</p>

<pre><code>&gt;&gt;&gt; joe.HasGoodMood = lambda: True
&gt;&gt;&gt; joe.SayHi()
Hello!
&gt;&gt;&gt; joe.Cash = 0
&gt;&gt;&gt; joe.SayHi()
Hello!
</code></pre>

<p>Cool. Notice how Python knows that when using the original implementation of <code>HasGoodMood</code>, it passes silently <code>self</code> as the first argument, but if I change it to <code>lambda: True</code>, it calls the function with no arguments. The problem is: What if I want to change the default <code>HasGoodMood</code> for another function which would also accept <code>self</code> as a parameter?</p>

<p>Let's continue our example: what if I want to create a greedy <code>Person</code> who is only happy if they have more than 100$ on them? I would like to do something like:</p>

<pre><code>&gt;&gt;&gt; greedy_jack = Person()
&gt;&gt;&gt; greedy_jack.HasGoodMood = lambda self: self.Cash &gt; 100
TypeError: &lt;lambda&gt;() missing 1 required positional argument: 'self'
</code></pre>

<p>Unfortunately, this does not work. Is there some other way to change a method?</p>

<hr>

<p><strong>Disclaimer:</strong> The above example is just for demonstration purposes. I know that I could use inheritance or keep a cash threshold as a property of the <code>Person</code>. But that is not the point of the question.</p>
