power of explicit self?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Dec 14 04:14:18 EST 2009


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



More information about the Python-list mailing list