[Python-Dev] Customizing the binding of attributes

Thomas Heller thomas.heller@ion-tof.com
Fri, 24 Aug 2001 09:00:31 +0200

From: "Guido van Rossum" <guido@python.org>
> I'm not sure what you are asking about, but let me give an example.
> You can write an auxiliary class that "describes" an instance
> attribute.  Instances of this "descriptor" class are stored in a class
> dict.  The descriptor class implements a method __get__ which is
> called in two situations:
> (a) when a class attribute is retrieved, <attr>.__get__(None, <class>)
>     is called; this can return an unbound method
> (b) when an instance attribute is retrieved, <attr>.__get__(<inst>, <class)
>     is called; this can return a bound method
> Does this help?

Yes, thanks. Exactly what I meant. You only forgot to mention that
the descriptor class must derive from object: is this intended?

I needed this to write

class X:
    method = myinstancemethod(func)

instead of

class X:
X.method = new.instancemethod(func, None, X)

where myinstancemethod is the descriptor class, and func
is any callable object (maybe a function implemented in C).

Two additional comments:

- Shouldn't (my)instancemethod be a builtin? Similar to
staticmethod and classmethod?

- The following seems to be a bug (remember, I'm on Windows):

Python 2.2a2 (#22, Aug 22 2001, 01:24:03) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class myinstancemethod(object):
...     def __init__(self, func):
...         self.func = func
...     def __get__(self, inst, klass=None):
...         import new
...         return new.instancemethod(self.func, inst, klass)
>>> class X:
...     meth = myinstancemethod(lambda *x: x)
>>> X().meth
<bound method X.<lambda> of <__main__.X instance at 007C7F04>>
>>> X().meth()
(<__main__.X instance at 007E80E4>,)
>>> X.meth
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
SystemError: NULL object passed to Py_BuildValue