What is "self"?

Ron Adam rrr at ronadam.com
Fri Sep 23 12:29:59 EDT 2005


Erik Max Francis wrote:
> Ron Adam wrote:
> 
>> When you call a method of an instance, Python translates it to...
>>
>>      leader.set_name(leader, "John")
> 
> 
> It actually translates it to
> 
>     Person.set_name(leader, "John")
> 

I thought that I might have missed something there.

Is there a paper on how python accesses and stores instance data and 
methods?  I googled but couldn't find anything that addressed this 
particular question.

 >>> class a(object):
...    def x(self):
...       print 'x'
...
 >>> b = a()
 >>> b
<__main__.a object at 0x009D1890>
 >>> b.x
<bound method a.x of <__main__.a object at 0x009D1890>>

So what exactly is a bound method object?  Does it possibly translates 
to something like the following?

     def x(*args, **kwds):
         self = ?
         return __class__.self(self, *args, **kwds)

Cheers,
Ron








More information about the Python-list mailing list