power of explicit self?

F

Fire Crow

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
 
G

Gabriel Genellina

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/gener...ed-explicitly-in-method-definitions-and-calls
 
J

John Roth

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

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
 
F

Fire Crow

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 }
 
B

Bruno Desthuilliers

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 !-)
 
C

Carl Banks

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
 
G

Gabriel Genellina

En Wed, 16 Dec 2009 11:56:17 -0300, Grant Edwards
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top