Example of inherited subclass from Python struct
<p>In reading the documentation for Python's <code>struct</code> module, the explanation for how to use a <a href="http://docs.python.org/2/library/struct.html#classes" rel="nofollow">struct Class</a> left a bit to be desired. That is, until I realized that it isn't meant as a class to be inherited from to create subclasses, as one might do with a <a href="http://docs.python.org/2/library/threading.html#thread-objects" rel="nofollow">threading.Thread class object</a>, for example. It was <a href="http://www.doughellmann.com/PyMOTW/struct/" rel="nofollow">Doug Hellmann's PyMOTW</a> that made this clear to me.</p>

<p>I'm curious though, is it possible to create <code>struct</code> subclasses? I would imagine these could be used to add functionality to the struct object, such as to create a format that depends on <em>the data stream itself</em>. One use for this could be a data stream that has a variable length field, the length of which is included in the early part of the stream. Or where certain fields are included or omitted depending on flags set in earlier fields.</p>

<p>For example, suppose I had the following data stream: </p>

<pre><code>&gt;&gt;&gt; data = '\x01\x00\x04\x73\x70\x61\x6d'
</code></pre>

<p>And I unpacked it with: </p>

<pre><code>&gt;&gt;&gt; struct.unpack('&gt;BH4s', data)
(1, 4, 'spam')
</code></pre>

<p>where the first element is a <code>data_type</code>, the second is a <code>length</code>, and the third is a variable length string, the length of which is specified by the second element.</p>

<p>If I wanted to create an 'unpacker' that would look at the <code>length</code> part to get information on how to unpack the rest of the data stream, would creating a <code>struct</code> subclass be a good way to go about it? Are there any examples of this sort of thing out there already?</p>
