Python classes - run method when any other method is called
<p>I am writing a Python app which will use a config file, so I am delegating the control of the config file to a dedicated module, <code>configmanager</code>, and within it a class, <code>ConfigManager</code>.</p>

<p>Whenever a method within <code>ConfigManager</code> is run, which will change my config file in some way, I will need to get the latest version of the file from the disk. Of course, in the spirit of DRY, I should delegate the opening of the config file to it's own function.</p>

<p>However, I feel as though explicitly calling a method to get and return the config file in each function that edits it is not very "clean".</p>

<p>Is there a recommended way in Python to run a method, and make a value available to other methods in a class, whenever and <em>before</em> a method is run in that class?</p>

<p>In other words:</p>

<ul>
<li><p>I create <code>ConfigManager.edit_config()</code>.</p></li>
<li><p>Whenever <code>ConfigManager.edit_config()</code> is called, another function <code>ConfigManager.get_config_file()</code> is run.</p></li>
<li><p><code>ConfigManager.get_config_file()</code> makes a value available to the method <code>ConfigManager.edit_config()</code>.</p></li>
<li><p>And <code>ConfigManager.edit_config()</code> now runs, having access to the value given by <code>ConfigManager.get_config_file()</code>.</p></li>
</ul>

<p>I expect to have many versions of <code>edit_config()</code> methods in <code>ConfigManager</code>, hence the desire to DRY my code.</p>

<p>Is there a recommended way of accomplishing something like this? Or should I just create a function to get the config fine, and manually call it each time?</p>

<p>The natural way to have:</p>

<blockquote>
  <p><code>ConfigManager.get_config_file()</code> makes a value available to the method
  <code>ConfigManager.edit_config()</code>.</p>
</blockquote>

<p>is to have <code>get_config_file()</code> <em>return</em> that value.</p>

<p>Just call <code>get_config_file()</code> within <code>edit_config()</code>.</p>

<p>If there are going to be <em>many</em> versions of <code>edit_config()</code>, then a decorator might be the way to go:</p>

<pre><code>def config_editor(func):
    def wrapped(self, *args, **kwargs):
        config_file = self.get_config_file()
        func(self, config_file, *args, **kwargs)
    return func

class ConfigManager
       .
       .
       .
    @config_editor
    def edit_config1(self, config_file, arg1):
           ...

    @config_editor
    def edit_config2(self, config_file, arg1, arg2):
           ...

ConfigManager mgr
mgr.edit_config1(arg1)
</code></pre>

<p>I don't actually like this:  </p>

<p>Firstly, the declaration of <code>edit_config1</code> takes one more argument than the actual usage needs (because the decorator supplies the additional argument).</p>

<p>Secondly, it doesn't actually save all that much boiler plate over:</p>

<pre><code>    def edit_config3(self, arg1):
        config_file = self.get_config_file()
</code></pre>

<p>In conclusion, I don't think the decorators save enough repetition to be worth it.</p>

python