On Thu, Aug 6, 2009 at 1:19 PM, alex23 <span dir="ltr"><<a href="mailto:wuwei23@gmail.com">wuwei23@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">Xavier Ho wrote:<br>
> You should subclass collections.UserDict, and not the default dict class.<br>
> Refer to the collections module.<br>
<br>
</div>Xavier, why do you think that is the correct approach?</blockquote><div><br>I'll be honest first and say that I do not completely understand how dict is implemented in the underlying C structure. But as Bruno had already mentioned, dict has a slightly different behaviour then we'd expect. For example, the __getitem__() function isn't actually used by the interpreter (which, you know, <i>can</i> be a problem.)<br>
<br><a href="http://www.python.org/download/releases/2.2.3/descrintro/#subclassing">http://www.python.org/download/releases/2.2.3/descrintro/#subclassing</a><br>(I don't know if 2.6.2 changed anything since 2.2.3, but there are some references you can look at, and sample code.)<br>
<br>To answer your question, it's really not "the correct approach", but I think (meaning: untested) UserDict doesn't have the same implementation, which might free up some restrictions Sergey encountered.<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">The docs say<br>
"The need for this class has been largely supplanted by the ability to<br>
subclass directly from dict (a feature that became available starting<br>
with Python version 2.2)."</blockquote><div><br>I didn't realise they took UserDict out in later versions (2.6, for example), and put it back in Python 3.0. Does anyone know why?<br><br>So, I do not actually know. <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
If you mean that Sergey should subclass _in this instance_ could you<br>
please explain why? (Sorry if you already have, I never saw your<br>
original post...)<br>
<div><div></div><div class="h5"></div></div></blockquote><div> <br>It was really more or less an educated guess. He didn't tell us what he is trying to achieve, so _in this instance_ I can't give more advice than saying "well, if this doesn't work, here are you other options that I know of/just found. Try these and let us know if you got it to work." *winks*<br>
<br>-<br><br>Sometimes I do answer a little too quickly, I apologise for that.<br><br>- Xav<br></div></div><br>