Why is getattr() not working like I think it should? I think this code should print 'sss'
<p>the next is my code:</p>

<pre><code>class foo:
    def __init__(self):
        self.a = "a"
    def __getattr__(self,x,defalut):
        if x in self:
            return x
        else:return defalut

a=foo()
print getattr(a,'b','sss')
</code></pre>

<p>i know the <code>__getattr__</code> must be 2 argument,but i want to get a default attribute if the attribute   is no being. </p>

<p>how can i get it, thanks</p>

<p><hr /></p>

<p>and </p>

<p>i found if defined <code>__setattr__</code>,my next code is also can't run</p>

<pre><code>class foo:
    def __init__(self):
        self.a={}
    def __setattr__(self,name,value):
            self.a[name]=value

a=foo()#error ,why
</code></pre>

<p><hr /></p>

<p>hi alex,
i changed your example:</p>

<pre><code>class foo(object):
    def __init__(self):
        self.a = {'a': 'boh'}
    def __getattr__(self, x):
        if x in self.a:
            return self.a[x]
        raise AttributeError

a=foo()
print getattr(a,'a','sss')
</code></pre>

<p>it print {'a': 'boh'},not 'boh'
i think it will  print self.a not self.a['a'], This is obviously not want to see</p>

<p>why ,and Is there any way to avoid it</p>
