Creating a hook to a frequently accessed object
<p>I have an application which relies heavily on a <code>Context</code> instance that serves as the access point to the context in which a given calculation is performed.</p>

<p>If I want to provide access to the <code>Context</code> instance, I can:</p>

<ol>
<li>rely on <code>global</code></li>
<li>pass the <code>Context</code> as a parameter to all the functions that require it</li>
</ol>

<p>I would rather not use <code>global</code> variables, and passing the <code>Context</code> instance to all the functions is cumbersome and verbose.</p>

<p>How would you "hide, but make accessible" the calculation <code>Context</code>?</p>

<p>For example, imagine that <code>Context</code> simply computes the state (position and velocity) of planets according to different data.</p>

<pre><code>class Context(object):
 def state(self, planet, epoch):
  """base class --- suppose `state` is meant
     to return a tuple of vectors."""
  raise NotImplementedError("provide an implementation!")

class DE405Context(Context):
"""Concrete context using DE405 planetary ephemeris"""
 def state(self, planet, epoch):
   """suppose that de405 reader exists and can provide
      the required (position, velocity) tuple."""
   return de405reader(planet, epoch)

def angular_momentum(planet, epoch, context):
 """suppose we care about the angular momentum of the planet,
    and that `cross` exists"""
 r, v = context.state(planet, epoch)
 return cross(r, v)

# a second alternative, a "Calculator" class that contains the context
class Calculator(object):

 def __init__(self, context):
  self._ctx = context

 def angular_momentum(self, planet, epoch):
  r, v = self._ctx.state(planet, epoch)
  return cross(r, v)

# use as follows:
my_context = DE405Context()
now = now() # assume this function returns an epoch
# first case:
print angular_momentum("Saturn", now, my_context)
# second case:
calculator = Calculator(my_context)
print calculator.angular_momentum("Saturn", now) 
</code></pre>

<p>Of course, I could add all the operations directly into "Context", but it does not feel right.</p>

<p>In real life, the <code>Context</code> not only computes positions of planets! It computes many more things, and it serves as the access point to a lot of data.</p>

<p>So, to make my question more succinct: how do you deal with objects which need to be accessed by many classes?</p>

<p>I am currently exploring: python's context manager, but without much luck. I also thought about dynamically adding a property "context" to all functions directly (functions are objects, so they can have an access point to arbitrary objects), i.e.:</p>

<pre><code>def angular_momentum(self, planet, epoch):
 r, v = angular_momentum.ctx.state(planet, epoch)
 return cross(r, v)

# somewhere before calling anything...
import angular_momentum
angular_momentum.ctx = my_context
</code></pre>

<h2>edit</h2>

<p>Something that would be great, is to create a "calculation context" with a <code>with</code> statement, for example:</p>

<pre><code> with my_context:
  h = angular_momentum("Earth", now)
</code></pre>

<p>Of course, I can already do that if I simply write:</p>

<pre><code> with my_context as ctx:
  h = angular_momentum("Earth", now, ctx) # first implementation above
</code></pre>

<p>Maybe a variation of this with the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow">Strategy pattern</a>?</p>
