C-like structures in Python
<p>Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like:</p>

<pre><code>class MyStruct():
    def __init__(self, field1, field2, field3):
        self.field1 = field1
        self.field2 = field2
        self.field3 = field3
</code></pre>

<p>Use a <a href="https://docs.python.org/2/library/collections.html#collections.namedtuple">named tuple</a>, which was added to the <a href="http://docs.python.org/library/collections.html">collections module</a> in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's <a href="http://code.activestate.com/recipes/500261/">named tuple</a> recipe if you need to support Python 2.4.</p>

<p>It's nice for your basic example, but also covers a bunch of edge cases you might run into later as well. Your fragment above would be written as:</p>

<pre><code>from collections import namedtuple
MyStruct = namedtuple("MyStruct", "field1 field2 field3")
</code></pre>

<p>The newly created type can be used like this:</p>

<pre><code>m = MyStruct("foo", "bar", "baz")
</code></pre>

<p>Or you can use named arguments:</p>

<pre><code>m = MyStruct(field1 = "foo", field2 = "bar", field3 = "baz")
</code></pre>
