Is this a super bug?
mis6 at pitt.edu
Mon Apr 21 23:22:47 CEST 2003
> "Bjorn Pettersen" <BPettersen at NAREX.com> wrote in message
> news:<mailman.1050811779.16487.python-list at python.org>...
> > <snip> this is the sequence that "should" happen:
> > 1 super(ff, f) # f == self
> > 2 superinst(type:<class ff>, obj:f)
> > 3 si.__getitem__(0)
> > 4 si.__getattr__(' getitem ')
> > 5 .. return str.__dict__[' getitem '].__get__(f)
> > 6 boundmethod(str.__getitem__, f)
> > 7 bmeth(0)
> > 8 'a'
> I don't understand what you are trying to say here ...
> Special method lookup goes through the class, and since the class is
> Super, not obj.__class__, they can't be trapped through __getattr__.
> (And for some reason they can't be trapped in a metaclass either...?)
?? Still unclear...
> Since Python has special attribute lookup rules for some attributes, the
> normal extension would be to define super as an object with data
> attributes from foo, and method attributes from Foobase. But that is not
> even correct, it has to have the method attributes from Foo, except when
> calling foo.m will call Foobase::m, but if Foobase::m calls m2, it will
> start lookup in Foo.
> One property this has is that given an empty subclass:
> class Foo : Foobase: pass
> and a program context C with a hole, C[*] (i.e. C gives the value of the
> program once you put a value in the hole), then:
> C[self] == C[super]
> proving properties in this half-baked scheme is much harder.
Not clear either ...
> recall that
> obj.meth.__get__(None, obj) is an unbound method
You mean obj.meth.__get__(None,ObjClass)
> m = obj.meth.__get__(ObjClass, obj) is a bound method
you mean m = obj.meth.__get__(obj,ObjClass)
> when saying obj.meth, you're doing the latter which means:
> 1. if the instance contains the bound method, return it.
> 2. if the class contains an unbound method with this name
> bind it to the object (create a bound method object
> and return it.
> And then the special case semantics:
> calls ObjClass.__getitem__(self, x) directly, not going through the
> steps above (thus my confustion that I can't trap it in the metaclass).
It seems working as a charm:
>>> class C(object):
... def __getitem__(self,a):
... return a
<bound method C.__getitem__ of <__main__.C object at 0x402f6c0c>>
Here obj[x] does not seems a special case ...
> Thus, if I ask for __getitem__ by name, we follow the general rules, if
> the __getitem__ semantics is invoked indirectly through subscripting, we
> follow the special rules.
The only special behavior I see (see my other posting) is that when __getitem__
is not defined c[x] gives a TypeError and not an AttributeError.
More information about the Python-list