how do i override imbedded function/definition values in python
<p>I apologize in advance if the terminology in my question is incorrect. I want to find out if it's possible to override a variable that is hidden deep in a bunch of consecutive Class calls that you don't have direct access too, but would still effect the outcome.</p>

<p>Here is an example I've tried to write that will hopefully make my question more clear.</p>

<p>I have 3 python modules, test1.py, test2.py and test3.py. Each module has a class and some definitions. Test2.py is where the code is run from.</p>

<p>Test1.py</p>

<pre><code>class A(object):
    def __init__(self, value=5):
        self.a = value
    def foo(self, value3 = 7, value4 = 5):
        from test3 import D
        c = self.a + value3 + value4
        d = D()
        e = d.B()
        f = c + e
        return f

class B(object):
    def __init__(self, value = 1):
        self.d = value
        object1 = A(self.d)
        self.fooReturn = object1.foo()
    def bar(self, value2 = 3):
        f = value2
        g = self.fooReturn + self.d + f
        return g
</code></pre>

<p>Test3.py</p>

<pre><code>class D(object):
    def __init__(self, value=6, wanted_value=9):
        self.value = value + wanted_value
    def A(self, value3 = 4):
        self.value3 = value3 * 2
        return self.value3
    def B(self, value4 = 5):
        valuex = self.A()
        value5 = value4 + self.value + valuex
        return value5
</code></pre>

<p>Test2.py</p>

<pre><code>from test1 import B

if __name__ == '__main__':
    test = B(4)
    result = test.bar()
    print result
</code></pre>

<p>Returns a value of:-</p>

<pre><code>55
</code></pre>

<p>I'm sorry it looks so convoluted, it's my way of trying to make a value look out of normal reach :)</p>

<p>Basically what I would like to do is find out how to redefine the value of 'wanted_value' in the Test3.py, class D, <strong>init</strong> def, while not changing the 'if' in Test2.py.</p>

<pre><code>if __name__ == '__main__':
    test = B(4)
    result = test.bar()
    print result
</code></pre>

<p>One way I can see might be possible would be to redefine the class D definition in Test2.py and redirect any call to the original class D to the modified one using a global variable.</p>

<p>e.g.</p>

<p>Test2.py</p>

<pre><code>from test1 import B
from test3 import D

class Dx(D):
    # add the value I want to use to 'wanted_value'
    def __init__(self, value=6, wanted_value=3):
        self.value = value + wanted_value

# create a global
D.__init__ = Dx.__init__

if __name__ == '__main__':
    test = B(4)
    result = test.bar()
    print result
</code></pre>

<p>This errors saying 'unbound method_<em>init</em>_() must be called with Dx instance as first arguement.</p>

<p>So it's obviously incorrect, but if these is a way to make it work with out having to redefine the whole of class D?</p>

<p>Any help would be great. It may be obvious that my knowledge of inheritance is not too crash hot :)</p>

<p>Thanks.</p>
