<div class="gmail_quote">On Mon, Jan 30, 2012 at 10:49 AM, Massimo Di Pierro <span dir="ltr"><<a href="mailto:massimo.dipierro@gmail.com">massimo.dipierro@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

STEP 4)<br>
 class Dummy(object):<br>
          def __getitem__(self,key): return getattr(self,key)<br>
          def __setitem__(self,key,value): return setattr(self,key,value)<br>
          def __getattr__(self,key):<br>
                  return object.__getattr__(self,key) if hasattr(self,key) else Dummy()<br>
<br>
Is this un-pythonic?<br>
<br>
I do not think so but I do have a problem with it:<br>
<br>
    class Dummy(object):<br>
        def __getitem__(self,key): return getattr(self,key)<br>
        def __setitem__(self,key,value): return setattr(self,key,value)<br>
        def __getattr__(self,key):<br>
            print 'wtf'<br>
            return object.__getattr__(self,key) if hasattr(self,key) else None<br>
<br>
    >>> d=Dummy()<br>
    >>> print d.somethingelse<br>
    wtf<br>
    ... 334 wtf times with python2.7, 999 times with python2.5...<br>
    wtf<br>
    None<br></blockquote><div><br></div><div>getattr calls hasattr, which calls getattr again. You are hitting the recursion limit before failing the test, due to hasattr failing with a recursion error, thus returning false. You could refactor this to use exceptions, such as:</div>

<div><br></div><div>def __getattr__(self, key):</div><div>    try:</div><div>        return object.__getattr__(self, key)</div><div>    except Exception:</div><div>        return None</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<br>
whatever this does internally, it makes some programs slower then I would like them to be. Why is it calling itself 334 times?<br>
<br>
STEP 5)<br>
We can add methods to make this object behave like a dictionary by redefining d.keys() in terms of d.__dict__.keys() etc.<br>
<br>
STEP 6)<br>
we can re-factor it a bit so that actually class Dummy is derived from dict.<br>
<br>
Is this the part that people do not like?<br></blockquote><div><br></div><div>The general discontent with the idea is step 4. Doing so leads to abnormalities, such as d['clear'] != d.clear. The only ways to resolve such abnormalities are to make using any keys/attributes which conflict illegal, or to use different access methods for the two forms.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
I would be happy if Python provided an efficient way to do something like STEP 4 without the problem I mentioned.<br>
Perhaps there is one and I ignore it. I do not necessarily require STEP5 and STEP6.<br>
<br>
Use case:<br>
<br>
settings = Dummy()<br>
settings.parameter1 = 'a'<br>
settings.parameter2 = 'b'<br>
etc.<br>
<br>
if not settings.parameter1: do something ...<br>
<span class="HOEnZb"><font color="#888888"><br>
Massimo</font></span></blockquote></div><br>