title: Accessing self from outside of a class
body: <p>I'm attempting to implement a decorator on certain methods in a class so that if the value has NOT been calculated yet, the method will calculate the value, otherwise it will just return the precomputed value, which is stored in an instance <code>defaultdict</code>. I can't seem to figure out how to access the instance <code>defaultdict</code> from inside of a decorator declared outside of the class. Any ideas on how to implement this?</p>
  
  <p>Here are the imports (for a working example):</p>
  
  <pre><code>from collections import defaultdict
  from math import sqrt
  </code></pre>
  
  <p>Here is my decorator:</p>
  
  <pre><code>class CalcOrPass:
      def __init__(self, func):
          self.f = func
  
      #if the value is already in the instance dict from SimpleData,        
      #don't recalculate the values, instead return the value from the dict
      def __call__(self, *args, **kwargs):
  
          # can't figure out how to access/pass dict_from_SimpleData to here :(
          res = dict_from_SimpleData[self.f.__name__]
          if not res:
              res = self.f(*args, **kwargs)
              dict_from_SimpleData[self.f__name__] = res
          return res
  </code></pre>
  
  <p>And here's the SimpleData class with decorated methods:</p>
  
  <pre><code>class SimpleData:
      def __init__(self, data):
          self.data = data
          self.stats = defaultdict() #here's the dict I'm trying to access
  
      @CalcOrPass
      def mean(self):
          return sum(self.data)/float(len(self.data))
  
      @CalcOrPass
      def se(self):
          return [i - self.mean() for i in self.data]
  
      @CalcOrPass
      def variance(self):
          return sum(i**2 for i in self.se()) / float(len(self.data) - 1)
  
      @CalcOrPass
      def stdev(self):
          return sqrt(self.variance())
  </code></pre>
  
  <p>So far, I've tried declaring the decorator <em>inside</em> of SimpleData, trying to pass multiple arguments with the decorator(apparently you can't do this), and spinning around in my swivel chair while trying to toss paper airplanes into my scorpion tank. Any help would be appreciated!</p>