What is "self"?

Diez B. Roggisch deets at nospam.web.de
Mon Sep 26 14:45:09 CEST 2005

> This still seems not quite right to me...  Or more likely seems to be 
> missing something still.
> (But it could be this migraine I've had the last couple of days 
> preventing me from being able to concentrate on things with more than a 
> few levels of complexity.)
> Playing around with the shell a bit gives the impression that calling a 
> method in a instance gives the following (approximate) result...
>     try:
>         leader.__dict__["set_name"]("John")
>     except:
>         type(leader).__dict__["set_name"].__get__(leader, "John")
>         # which results in...
>         #    Person.set_name(leader, "John")
>     except:
>         raise( AttributeError,
>                "%s object has no attribute %s" \
>                      % (leader, "set_name") )
> Of course this wouldn't use the object names directly... I guess I'll 
> need to look in the C object code to see exactly how it works.  But the 
> links you gave help.

I guess you mean to indent the whole part after the first except and put 
a try beforehand?

Apart from that you seem to be right - there can very well be values in 
the class dict that don't follow the descriptor-protocol. However my 
playing around with this stuff indicates that the creation of bound 
methods relies on the method being wrapped in a descriptor - otherwise, 
you get the notorious TypeError

set_name() takes exactly 1 argument (0 given)

as the binding doesn't occur.



More information about the Python-list mailing list