Is it OK to replace a method by a plain function?
<p>This works as expected, but I am somehow unsure about this approach. Is it safe? Is it pythonic?</p>

<pre><code>class Example:
    def __init__(self, parameter):
        if parameter == 0:
            # trivial case, the result is always zero
            self.calc = lambda x: 0.0  # &lt;== replacing a method
        self._parameter = parameter

    def calc(self, x):
        # ... long calculation of result ...
        return result
</code></pre>

<p>(If there is any difference between Python2 and Python3, I'm using Python3 only.)</p>

<p>You'll have a problem should <code>parameter</code> ever changes, so I don't consider it good practice.
Instead, I think you should do this:</p>

<pre><code>class Example:
    def __init__(self, parameter):
        self._parameter = parameter

    def calc(self, x):
        if not self._parameter:
            return 0.0
        # ... long calculation of result ...
        return result
</code></pre>

python