title: Hook type casting to dict in Python
body: <p>In a nutshell I have a class <code>Session</code> inherited from <code>dict</code> (in fact, this class overrides <code>flask.SecureCookieSession</code> class). Also I have a client code that's doing something like this:</p>
  
  <pre><code>session_data = dict(session)
  </code></pre>
  
  <p>And I want to somehow hook this <code>to dict</code> conversion in order to inject few lines of code modifying session data before serializing. How can I do it in Python?</p>
  
  <p><strong>UPD:</strong> To clarify: I don't want to return anything different from a dict object from the <code>dict()</code> call. I just want this call invokes a special method of my session object before getting a data to convert it to the dict object. Something like <code>__to_dict(self)</code> hook.</p>