[Python-Dev] Attribute lookup (was Re: python-dev Summary for 2003-05-01 through 2003-05-15) (original) (raw)

Bjorn Pettersen BPettersen@NAREX.com
Tue, 20 May 2003 13:28:18 -0600


From: Phillip J. Eby [mailto:pje@telecommunity.com]=20

[attribute lookup...]

> > Also note that special methods are not handled specially here. > > The behavior Aahz is referring to is that slots (e.g. tpcall) on > > new-style types do not retrieve an instance attribute; they are > > based purely on class-level data. >[...] > >Wouldn't that be explicitly specified class-level data, i.e. it >circumvents the getattr hook completely: =20 I was focusing on the documenting the attribute lookup=20 behavior, not the "special methods" behavior. :) =20

Fair enough :-)

My point was only that "special methods" aren't implemented=20 via attribute lookup, so the attribute lookup rules don't apply.

Very true, although I don't think I could find that in the documentation anywhere... RefMan 3.3 paragraph 1, last sentence "Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined." comes close, but seems to be contradicted by the "getattr" documentation in 3.3.2.

[..implementing len through getattr..]

>The meta example would have to work to be able to create "true" proxy >objects(?) =20 You can always do this: =20 class C(object): def len(self): return self.getLength() =20 def getattr(self,attr): if attr=3D=3D'getLength': return lambda: 42 =20 if you really need to do that.

Well... no. E.g. a general RPC proxy might not know what it needs to special case:

class MyProxy(object): def init(self, server, objID, credentials): self.__obj =3D someRPClib.connect(server, objID, credentials)

 def __getattr__(self, attr):
     def send(*args, **kw):
         self.__obj.remoteExec(attr, args, kw)
     return send

Do you mean defining "stub" methods for all the special methods? (there are quite a few of them...)

>Is this intended behavior? =20 You'd have to ask Guido that.

:-) The reason I ask is that I'm trying to convert a compiler.ast graph into a .NET CodeDom graph, and the current behavior seemed unnecessarily restrictive...

-- bjorn