Controlling getter and setter for a python's class
<p>Consider the following class :</p>

<pre><code>class Token:
    def __init__(self):
        self.d_dict = {}

    def __setattr__(self, s_name, value):
        self.d_dict[s_name] = value

    def __getattr__(self, s_name):
        if s_name in self.d_dict.keys():
            return self.d_dict[s_name]
        else:
            raise AttributeError('No attribute {0} found !'.format(s_name))
</code></pre>

<p>In my code Token have some other function (like get_all() wich return d_dict, has(s_name) which tell me if my token has a particular attribute).</p>

<p>Anyway, I think their is a flaw in my plan since it don't work : when I create a new instance, python try to call <code>__setattr__('d_dict', '{}')</code>.</p>

<p>How can I achieve a similar behaviour (maybe in a more pythonic way ?) without having to write something like Token.set(name, value) and get(name) each I want to set or get an attribute for a token.</p>

<p>Critics about design flaw and/or stupidity welcome :)</p>

<p>Thank !</p>
