[Python-3000] Method descriptors

Marcin ‘Qrczak’ Kowalczyk qrczak at knm.org.pl
Mon Dec 10 23:57:26 CET 2007


Dnia 10-12-2007, Pn o godzinie 11:01 -0800, Guido van Rossum pisze:

> Add a __get__ (instance) method to f's class, and store f directly in
> A. Your __get__ method should return a bound object using
> PyMethod_New(f, a).

Thank you, but I can't do this, because I want a generic mechanism which
works for an arbitrary f (which will probably be a Kogut object wrapped
in Python, and not all such objects play the role of methods but they
use the same type).

I solved this by a separate Python type, which is a simple wrapper not
specific to my other types, so I'm a bit surprised that there is no
such type among various wrappers and descriptors in the Python core
(or perhaps I overlooked it somewhere). It wraps a single object f, and
its get_descr(m, obj, type) returns PyMethod_New(f, obj) (for Python3)
or PyMethod_New(f, obj, type) (for Python2), except that if obj == NULL,
it returns f itself.

It's even better than PyMethod_New(f, NULL, type) in that it does not
need to know the class when created.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/



More information about the Python-3000 mailing list