Leo Soto M. wrote:
> On Fri, Jun 27, 2008 at 5:01 PM, Leo Soto M. <leo.soto@...> wrote:
>> On Fri, Jun 27, 2008 at 4:13 PM, Samuele Pedroni <pedronis@...> wrote:
>>> at the moment it seems that is solved by having the *Derived classes
>>> implement both __finditem__ and __getitem__ as appropriate instead of
>>> relying on the default implementation in PyObject. I personally don't think
>>> that's a bad solution.
>> It's not good either. Failed lookups will run the lookup code twice.
>> That's why I discarded a previous patch for __getattr__/__findattr__
>> which followed the same strategy.
>> But thinking this again looks like I could have avoided the double
>> lookup by copying the trivial PyObject#__getitem__ code to
> To make it more clear: I was talking about removing the super call on
> But now I see that this isn't a good idea, because it would break the
> principle of least surprise if __getitem__ is overrided on other
> classes. For example, if we implement PyFoo#__getitem__, it wouldn't
> be invoked on PyFooDerived instances.
but apart PyInstance which is a special case anyway and has no Derived
class, all classes that override
__getitem__ do so to control the raised exception, which means that a
slight refactoring of PyObject.__getitem__
in terms of an helper lookup_error or so, would allow to avoid
overriding __getitem__ completely. Subclasses
of PyObject would simply replace the error condition helper. This is
just one of the possible approaches.
It would reduce code here and there too.
> So, it looks like I'm going in circles, because "correctness first"
> would put me again to the "lets change __finditem__ to call
> __getitem__" camp.
But it seems that in all cases, because of the semantics of Java
collections, defining finditem is the natural and fast thing,
exept for PyInstance and Derived classes.
On a related/unrelated note skimming the code of PyDictionary it seems
that the semantics of the python level __getitem__ (dict__getitem__) and
__finditem__ for it got out of sync, __finditem__ will not respect
__missing__. I may be confused though.