function decorators to specify the first two lines of the def?
<p>I'm on python 3.5:</p>

<p>I have a repeating pattern in some of my python functions. For a large collection of classes the first two lines are:</p>

<pre><code>obj_a = &lt;..... obtain something I need.....&gt;
obj_b = &lt;..... obtain another thing I need....&gt;
</code></pre>

<p>I'm simplifying it here, but the process of obtaining <code>obj_a</code> and <code>obj_b</code> isn't a one liner... I would like to avoid repeating this code anywhere in a more elegant way than a util function to obtain <code>obj_a</code> and <code>obj_b</code> (e.g. <code>obj_a = getObjectA()</code>...) </p>

<p>Is there any way to take those lines and put them as part of a decorator of a function where I have something like:</p>

<pre><code>@function_where_I_need_my_objects
def foo:
  &lt;....do something with obj_a and obj_b already initialized....&gt;
</code></pre>

<p>You may consider using <em>callable classes</em> instead of functions, this way you could keep the boilerplate in one place, and implement the other stuff as subclasses of that base.  </p>

<pre><code>class BaseFoo:

    def __init__(self):
        # self.obj_a = &lt;..... obtain something I need.....&gt;
        # self.obj_b = &lt;..... obtain another thing I need....&gt;

class MyFoo(BaseFoo):

    def __call__(self, *arg, **kwargs):
        # &lt;....do something with obj_a and obj_b already initialized....&gt;

foo = MyFoo()
</code></pre>

python python-3.x