How can I override property setting for a dict-like object?
<p>I'm writing a C binding, and the C structure that I'm wrapping has some string-indexed property values.</p>

<p>I'd like to expose these as a dict in python.</p>

<p>So far, I've got a function <code>get_properties</code>, which returns all the object's properties as a single dict.  I wrapped this using the <code>property</code> function in the class definition, so that I can access it as a class attribute:</p>

<p>(in class definition)</p>

<pre><code>class MyClass:
    def get_properties(self):
        ...
    properties = property(get_properties)
</code></pre>

<p>(example)</p>

<pre><code>&gt;&gt;&gt; print myobj.properties
{'test': 5, 'test2': 'string'}
</code></pre>

<p>Now, I'd like to handle setting them in a dict-like way.  I have a wrapper function for the C function called <code>set_property</code>, which takes a string key and a value of several types.</p>

<p>I tried using <code>set_properties</code> from my class <code>property</code>:</p>

<pre><code>class MyClass:
    def get_properties(self):
        ...
    def set_property(self, key, value):
        ...
    def set_properties(self, props):
        [self.set_property(k, props[k]) for k in props]
    properties = property(get_properties, set_properties)
</code></pre>

<p>This works like this following:</p>

<pre><code>&gt;&gt;&gt; myobj.properties = {"test3": 6}
&gt;&gt;&gt; print myobj.properties
{'test': 5, 'test2': 'string', 'test3': 6}
</code></pre>

<p>However, as you can see it's not entirely the expected behaviour.  What I'd prefer is something like:</p>

<pre><code>&gt;&gt;&gt; myobj.properties['test3'] = 6
</code></pre>

<p>I tried adding a definition for <code>__setitem__</code> to <code>properties</code>:</p>

<pre><code>class MyClass:
    ...
    properties = property(get_properties)
    properties.__setitem__ = set_property
</code></pre>

<p>But this got me,</p>

<pre><code>AttributeError: 'property' object has no attribute '__setitem__'
</code></pre>

<p>I tried to make property a dict and simply override <code>__setitem__</code> and <code>__getitem__</code> but it wouldn't have it.</p>

<p>Any idea what the correct way to do this is?  Can I make a class <code>property</code> behave like a dictionary?</p>

<p>Thanks.</p>
