An object is an instance (or not)?

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Jan 30 10:07:57 CET 2015


Steven D'Aprano wrote:
> Actually, if you look at my example, you will see that it is a method and it 
> does get the self argument. Here is the critical code again:
> 
> from types import MethodType
> polly.talk = MethodType(
>     lambda self: print("Polly wants a spam sandwich!"), polly)

Doing it by hand is cheating.

> That's certainly not correct, because Python had classes and instances 
> before it had descriptors!

Before the descriptor protocol, a subset of its functionality
was hard-wired into the interpreter. There has always been
some magic going on across the instance-class boundary that
doesn't occur across the class-baseclass boundary.

> Ah wait, I 
> think I've got it. If you want (say) your class object itself to support 
> (say) the + operator, it isn't enough to write a __add__ method on the 
> class, you have to write it on the metaclass.

That's right.

-- 
Greg



More information about the Python-list mailing list