title: Represent a class as a dict or list
body: <p>I've classes that is used for getting data from one system, making some modifications and then outputting them into another system. Which usually goes the way of converting it into a <code>dict</code> or a <code>list</code> after I've made all the necessary conversions.</p>
  
  <p>So far what I've done is that I've made two methods called <code>as_dict()</code> and <code>as_list()</code> and used that whenever I need that representation. </p>
  
  <p>But I'm curious if there's a way to be able to do <code>dict(instance_of_my_class)</code> or <code>list(instance_of_my_class)</code>.</p>
  
  <p>I've been reading up on <a href="http://www.rafekettler.com/magicmethods.html">magic methods</a> and it seems as if this is not possible?</p>
  
  <p>And some simple sample code to work with:</p>
  
  <pre><code>class Cost(object):
      @property
      def a_metric(self):
          return self.raw_data.get('a_metric', 0) * 0.8
  
      [..]
      # Repeat for various kinds of transformations
  
      def as_dict(self):
          return {
            'a_metric': self.a_metric,
            [...]
          }
  </code></pre>