title: In python, how do I cast a class object to a dict
body: <p>Let's say I've got a simple class in python</p>
  
  <pre><code>class Wharrgarbl(object):
      def __init__(self, a, b, c, sum, version='old'):
          self.a = a
          self.b = b
          self.c = c
          self.sum = 6
          self.version = version
  
      def __int__(self):
          return self.sum + 9000
  
      def __what_goes_here__(self):
          return {'a': self.a, 'b': self.b, 'c': self.c}
  </code></pre>
  
  <p>I can cast it to an integer very easily</p>
  
  <pre><code>&gt;&gt;&gt; w = Wharrgarbl('one', 'two', 'three', 6)
  &gt;&gt;&gt; int(w)
  9006
  </code></pre>
  
  <p>Which is great! But, now I want to cast it to a dict in a similar fashion</p>
  
  <pre><code>&gt;&gt;&gt; w = Wharrgarbl('one', 'two', 'three', 6)
  &gt;&gt;&gt; dict(w)
  {'a': 'one', 'c': 'three', 'b': 'two'}
  </code></pre>
  
  <p>What do I need to define for this to work? I tried substituting both <code>__dict__</code> and <code>dict</code> for <code>__what_goes_here__</code>, but <code>dict(w)</code> resulted in a <code>TypeError: Wharrgarbl object is not iterable</code> in both cases. I don't think simply making the class iterable will solve the problem. I also attempted many googles with as many different wordings of "python cast object to dict" as I could think of but couldn't find anything relevant :{</p>
  
  <p>Also! Notice how calling <code>w.__dict__</code> won't do what I want because it's going to contain <code>w.version</code> and <code>w.sum</code>. I want to customize the cast to <code>dict</code> in the same way that I can customize the cast to <code>int</code> by using <code>def int(self)</code>.</p>
  
  <p>I know that I could just do something like this</p>
  
  <pre><code>&gt;&gt;&gt; w.__what_goes_here__()
  {'a': 'one', 'c': 'three', 'b': 'two'}
  </code></pre>
  
  <p>But I am assuming there is a pythonic way to make <code>dict(w)</code> work since it is the same type of thing as <code>int(w)</code> or <code>str(w)</code>. If there isn't a more pythonic way, that's fine too, just figured I'd ask. Oh! I guess since it matters, this is for python 2.7, but super bonus points for a 2.4 old and busted solution as well.</p>
  
  <p>There is another question <a href="https://stackoverflow.com/questions/23252370/overloading-dict-on-python-class">Overloading __dict__() on python class</a> that is similar to this one but may be different enough to warrant this not being a duplicate. I believe that OP is asking how to cast all the data in his class objects as dictionaries. I'm looking for a more customized approach in that I don't want everything in <code>__dict__</code> included in the dictionary returned by <code>dict()</code>. Something like public vs private variables may suffice to explain what I'm looking for. The objects will be storing some values used in calculations and such that I don't need/want to show up in the resulting dictionaries.</p>
  
  <p>UPDATE:
  I've chosen to go with the <code>asdict</code> route suggested but it was a tough choice selecting what I wanted to be the answer to the question. Both @RickTeachey and @jpmc26 provided the answer I'm going to roll with but the former had more info and options and landed on the same result as well and was upvoted more so I went with it. Upvotes all around though and thanks for the help. I've lurked long and hard on stackoverflow and I'm trying to get my toes in the water more.</p>