power of explicit self?

3 views
Skip to first unread message

Fire Crow

unread,
Dec 11, 2009, 11:04:42 PM12/11/09
to
I'm looking for an explanation of how explicit self is implimented and
what features are only possible because of, or are greatly improved,
because of it. I've always liked explicit self and am looking for the
computer science behind it, so that I can explain the benefits that I
see.

I'm also interested in the files/lines of the python source that shows
how explicit self is implemented if anyone can point out where that
takes place.

all help welcome

Gabriel Genellina

unread,
Dec 12, 2009, 12:53:13 AM12/12/09
to pytho...@python.org
En Sat, 12 Dec 2009 01:04:42 -0300, Fire Crow <m...@firecrow.com> escribi�:

> I'm looking for an explanation of how explicit self is implimented and
> what features are only possible because of, or are greatly improved,
> because of it. I've always liked explicit self and am looking for the
> computer science behind it, so that I can explain the benefits that I
> see.

See the FAQ [1]

> I'm also interested in the files/lines of the python source that shows
> how explicit self is implemented if anyone can point out where that
> takes place.

Nowhere, I'd say. An *implicit* self would have to be implemented
somewhere in the compiler -- but an explicit self doesn't. It's
homogeneous, always name-dot-attribute; the name 'self' is not special at
all.

[1]
http://www.python.org/doc/faq/general/#why-must-self-be-used-explicitly-in-method-definitions-and-calls

--
Gabriel Genellina

John Roth

unread,
Dec 12, 2009, 9:35:32 AM12/12/09
to

It's not implemented in the compiler. There's a place in the runtime
for invoking a method where the object is inserted at the beginning
of the parameter list. IIRC, that's done by wrapping the function
object.

John Roth

Fire Crow

unread,
Dec 12, 2009, 3:20:06 PM12/12/09
to
> It's not implemented in the compiler. There's a place in the runtime
> for invoking a method where the object is inserted at the beginning
> of the parameter list. IIRC, that's done by wrapping the function
> object.

This is the source of Objects/methodobject.c it look like this is
where
self is added to the argument list, but I'll have to do some more
digging.
thanks for the tip.


50 PyObject *
51 PyCFunction_GetSelf(PyObject *op)
52 {
53 if (!PyCFunction_Check(op)) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
57 return ((PyCFunctionObject *)op) -> m_self;
58 }
....
69
70 PyObject *
71 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
72 {
...
75 PyObject *self = PyCFunction_GET_SELF(func);
...
78 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS |
METH_STATIC |
METH_COEXIST)) {
79 case METH_VARARGS:
80 if (kw == NULL || PyDict_Size(kw) == 0)
81 return (*meth)(self, arg);
82 break;
83 case METH_VARARGS | METH_KEYWORDS:
...
126 }

Tim Roberts

unread,
Dec 13, 2009, 10:37:40 PM12/13/09
to
Fire Crow <m...@firecrow.com> wrote:

>> It's not implemented in the compiler. There's a place in the runtime
>> for invoking a method where the object is inserted at the beginning
>> of the parameter list. IIRC, that's done by wrapping the function
>> object.
>
>This is the source of Objects/methodobject.c it look like this is
>where self is added to the argument list, but I'll have to do some
>more digging.

Well, that's where the owning object is added, but that would be required
whether "self" were implicit or explicit. C++ would have the same kind of
code to push "this", for example, even though "this" is not shown in the
parameter list.

I agree with the other repliers. "Explicit" self is not "implemented"
anywhere. It's just an implementation decision.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Bruno Desthuilliers

unread,
Dec 14, 2009, 4:14:18 AM12/14/09
to
Fire Crow a �crit :

> I'm looking for an explanation of how explicit self is implimented

It's not "implemented" - it's just the first argument of the function,
and you have to declare it explicitely in the function's args list.

If your question is about how "obj.func()" becomes
"obj.___class__.func(obj)", then the answer is "protocol deescriptor" +
"method type". The function type implements the protocol descriptor so
that its __get__ method returns a method instance which wraps around the
function, class and instance. Then the method's __call__ method will
delegate to the function, injecting the instance as first positional param.

> and
> what features are only possible because of, or are greatly improved,
> because of it.

Simplicity (methods are built of the combination of two "general
purpose" features - descriptor protocol and callable objects),
flexibility (you can use any callable object as a 'method' as long as it
correctly implements the descriptor protocol), and of course readability
(no special rules wrt/ 'self', it's nothing else than the first argument
of the function, period).

Oh, and yes - you can use methods as functions too, it's sometimes handy
for dispatching purposes !-)

Carl Banks

unread,
Dec 14, 2009, 7:56:12 AM12/14/09
to
On Dec 12, 12:20 pm, Fire Crow <m...@firecrow.com> wrote:
> > It's not implemented in the compiler. There's a place in the runtime
> > for invoking a method where the object is inserted at the beginning
> > of the parameter list. IIRC, that's done by wrapping the function
> > object.
>
> This is the source of Objects/methodobject.c it look like this is
> where
> self is added to the argument list, but I'll have to do some more
> digging.

No, not really. That code sets the self argument only for functions
implemented in C.

The code that implements self behavior for Python methods is mostly
found in the file classobject.c. Basically whenever a method is
accessed through an object, the object creates an instancemethod for
it. The instancemethod type is defined in classobject.c.


Carl Banks

Gabriel Genellina

unread,
Dec 16, 2009, 9:41:09 AM12/16/09
to Fire Crow, python-list
Fire Crow <m...@firecrow.com>:

> > Nowhere, I'd say. An *implicit* self would have to be implemented
> > somewhere in the compiler -- but an explicit self doesn't. It's
> > homogeneous, always name-dot-attribute; the name 'self' is not special at
> > all.

> This is I find very interesting, If I understand your comment
> correctly,
> one of the advantages of implicit self is that it does not need

> any special treatment, it's handled like any other name-dot-attribute.


Yes.

--
Gabriel Genellina


Yahoo! Cocina

Encontra las mejores recetas con Yahoo! Cocina.


http://ar.mujer.yahoo.com/cocina/

Grant Edwards

unread,
Dec 16, 2009, 9:56:17 AM12/16/09
to
On 2009-12-16, Gabriel Genellina <gags...@yahoo.com.ar> wrote:
> Fire Crow <m...@firecrow.com>:
>
>> > Nowhere, I'd say. An *implicit* self would have to be implemented
>> > somewhere in the compiler -- but an explicit self doesn't. It's
>> > homogeneous, always name-dot-attribute; the name 'self' is not special at
>> > all.
>
>> This is I find very interesting, If I understand your comment
>> correctly, one of the advantages of implicit self is that it
>> does not need any special treatment, it's handled like any
>> other name-dot-attribute.
>
> Yes.

I presume you both meant that is an advantage of explicit self
not implicit?

--
Grant Edwards grante Yow! What's the MATTER
at Sid? ... Is your BEVERAGE
visi.com unsatisfactory?

Gabriel Genellina

unread,
Dec 16, 2009, 10:35:26 AM12/16/09
to pytho...@python.org
En Wed, 16 Dec 2009 11:56:17 -0300, Grant Edwards
<inv...@invalid.invalid> escribi�:

> On 2009-12-16, Gabriel Genellina <gags...@yahoo.com.ar> wrote:
>> Fire Crow <m...@firecrow.com>:
>>
>>> > Nowhere, I'd say. An *implicit* self would have to be implemented
>>> > somewhere in the compiler -- but an explicit self doesn't. It's
>>> > homogeneous, always name-dot-attribute; the name 'self' is not
>>> special at
>>> > all.
>>
>>> This is I find very interesting, If I understand your comment
>>> correctly, one of the advantages of implicit self is that it
>>> does not need any special treatment, it's handled like any
>>> other name-dot-attribute.
>>
>> Yes.
>
> I presume you both meant that is an advantage of explicit self
> not implicit?

Yes, sorry, the advantage of an *explicit* self is being homogeneous.

--
Gabriel Genellina

Reply all
Reply to author
Forward
0 new messages