How to change class implementation efficiently
<p>I'd like to change the implementation depending on a constructor argument. Below is an example showing what I mean:</p>

<pre><code>class Device(object):
    def __init__(self, simulate):
        self.simulate = simulate

    def foo(self):
        if simulate:
            self._simulate_foo()
        else:
            self._do_foo()

    def _do_foo(self):
        # do foo

    def _simulate_foo(self):
        # simulate foo
</code></pre>

<p>Now every call to <code>foo()</code> invokes an <code>if</code> clause. To avoid that I could bind the correct method dynamically to <code>foo</code>.</p>

<pre><code>class Device(object):
    def __init__(self, simulate):
        if simulate:
            self.foo = self._simulate_foo
        else:
            self.foo = self._do_foo()

    def _do_foo(self):
        # do foo

    def _simulate_foo(self):
        # simulate foo
</code></pre>

<p>Are there any drawbacks why this should not be done or other drawbacks I'm not aware? Is this really faster?(I'm aware that inheritance is another option)</p>
