On Nov 27, 2007 2:16 AM, Nick Coghlan <[EMAIL PROTECTED]> wrote:
> Greg Ewing wrote:
> > Guido van Rossum wrote:
> >> They have that too. See:
> >>
> >>>>> f = list.append
> >>>>> g = f.__get__(a)
> >>>>> g
> >> <built-in method append of list object at 0x590f8>
> >
> > Hmmm. It seems that C method objects are more
> > like an unbound method object that's pre-bound to a
> > particular class.
> >
> > I'm concerned with built-in function objects that
> > are *not* methods of anything. I'm suggesting they
> > should behave the same way as a Python function when
> > you put one in a class, i.e. accessing it through an
> > instance creates a bound method.
>
> I think I see what you're getting at now - adding a '__get__' method to
> the basic C function wrapper that returns self when retrieved from the
> class, and a bound instancemethod when retrieved from an instance.
>
> To illustrate the difference between builtin functions and methods:
>
>  >>> type(str.lower)
> <type 'method_descriptor'>
>  >>> '__get__' in dir(str.lower)
> True
>
>  >>> type(map)
> <type 'builtin_function_or_method'>
>  >>> '__get__' in dir(map)
> False
>
> This seems like an eminently sensible idea to me, and not really
> something that can be done in the 2.x series.

On second thought (after responding to Greg), I agree that it is
sensible. But why can't it be done in 2.6?

The attached implementation is simple enough...

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
Index: Objects/methodobject.c
===================================================================
--- Objects/methodobject.c	(revision 59193)
+++ Objects/methodobject.c	(working copy)
@@ -157,6 +157,19 @@
 	return self;
 }
 
+static PyObject *
+meth_descr_get(PyObject *m, PyObject *obj, PyObject *type)
+{
+	if (((PyCFunctionObject *)m)->m_self != NULL ||
+	    obj == NULL || obj == Py_None)
+	{
+		Py_INCREF(m);
+		return m;
+	}
+	return PyMethod_New(m, obj);
+}
+
+
 static PyGetSetDef meth_getsets [] = {
 	{"__doc__",  (getter)meth_get__doc__,  NULL, NULL},
 	{"__name__", (getter)meth_get__name__, NULL, NULL},
@@ -264,6 +277,8 @@
 	meth_getsets,				/* tp_getset */
 	0,					/* tp_base */
 	0,					/* tp_dict */
+	meth_descr_get,				/* tp_descr_get */
+	0,					/* tp_descr_set */
 };
 
 /* Find a method in a method chain */
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to