What is "self"?

Ron Adam rrr at ronadam.com
Sun Sep 25 19:07:00 CEST 2005


Michael Spencer wrote:

> All is explained at:
> http://users.rcn.com/python/download/Descriptor.htm#functions-and-methods
> and further at:
> http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf
> 
> "For objects, the machinery is in object.__getattribute__ which 
> transforms b.x into type(b).__dict__['x'].__get__(b, type(b))."
> 
> What follows is my interpretation - hope it's correct:
> 
> # what exactly is a bound method object?
> # Illustrate b.f => type(b).__dict__['x'].__get__(b, type(b))
> 
>  >>> class B(object):
>  ...     def f(self, x):
>  ...         return x or 42
>  ...
>  >>> b = B()
>  >>> type(b).__dict__['f']
>  <function f at 0x015052B0>  # a plain old function
>  >>> _.__get__(b, type(b))   # invoke the descriptor protocol
>                              # to make a bound method
>  <bound method B.f of <Untitled7.B object at 0x01843D70>>
>  >>>

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.

Thanks,
Ron














More information about the Python-list mailing list