<br><br><div class="gmail_quote">On 23 May 2011 19:16, Michael Foord <span dir="ltr"><<a href="mailto:fuzzyman@gmail.com">fuzzyman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello all,<div><br></div><div>I'm looking at implementing __dir__ for a class (mock.Mock as it happens) to include some dynamically added attributes, the canonical use case according to the documentation:</div><div><br>

</div><div>    <a href="http://docs.python.org/dev/reference/datamodel.html?highlight=__dir__#object.__dir__" target="_blank">http://docs.python.org/dev/reference/datamodel.html?highlight=__dir__#object.__dir__</a></div>
<div><br></div><div>
What I would like to do is report all the "standard attributes", and then add any dynamically created attributes.</div><div><br></div><div>So the question is, how do I obtain the "standard list" (the list that dir would normally report in the absence of a custom __dir__ implementation)?</div>

<div><br></div><div>There is no object.__dir__ (despite the fact that this is how it is documented...) and obviously calling dir(self) within __dir__ is doomed to failure.</div><div><br></div><div>The best I have come up with is:</div>

<div><br></div><div>def __dir__(self):</div><div>    return dir(type(self)) + list(self.__dict__) + self._get_dynamic_attributes()</div></blockquote><div><br></div><div><br></div><div>Better version which orders and removes duplicates:</div>
<div><br></div><div><div>        return sorted(set((dir(type(self)) + list(self.__dict__) + self._get_dynamic_attributes()))</div><meta charset="utf-8"></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><br></div><div>This works (absent multiple inheritance), but it would be nice to just be able to do:</div>
<div><br></div><div>def __dir__(self):</div><div>    standard = super().__dir__()</div><div>    return standard + self._get_dynamic_attributes()</div><div><br></div><div>Moving the relevant parts of the implementation of dir into object.__dir__ would be one way to solve that.</div>

<div><br></div><div>All the best,</div><div><br></div><div>Michael Foord</div><div><br>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br>
<br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>


<br>
</div>
</blockquote></div><br><br clear="all"><br>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
<br>