title: Sorting a dictionary of objects by the result of algebraic operation
body: <p>Say that, I have this class defined:</p>
  
  <pre><code>class A:
      def __init__(self, x, y):
          self.x = x
          self.y = y
  </code></pre>
  
  <p>Assuming attributes x, y hold integer values, I have a dictionary of 'A' objects - let's call 'a_dict'. I want to sort this dictionary based on the result of an algebraic operation by x and y. I tried this:</p>
  
  <pre><code>a_dict_sorted = sorted(a_dict.values(), key=100*int(operator.attrgetter('x')) / (float(operator.attrgetter('x')) + float(operator.attrgetter('y')))
  </code></pre>
  
  <p>Apparently <code>operator.attrgetter</code> returns a callable object, rather than the value of that attribute.  So the code above gives this error:</p>
  
  <pre><code>TypeError: int() argument must be a string or a number, not 'operator.attrgetter'
  </code></pre>
  
  <p>Is there any gotcha to sort a dictionary like that? Or I have to preprocess it to create the algebraic results first then sort accordingly?</p>