Class method ptrs and dictionaries

Emile van Sebille emile at fenx.com
Fri Apr 19 14:56:00 EDT 2002


Shane Hoversten
[snip]
>  >>> a = foo.foo()
>  >>> a.bar()
> printOne: Hello, what!
> printTwo: Hello, what! I say!
> I'm in nothing.
>
> My question: how does python "know" that the functions I'm putting in
the
> dictionary are class methods that need to be handed a self pointer in
> the first two cases, and not handed one in the third case?  Are these
> issues described somewhere that somebody could point me to so I could
> read about how python handles this behind the scenes?  I did a couple
> quick searches but didn't turn anything up...
>

If you look at the def's involved, you'll see that they're recognized
differently:

>>> a = foo()
>>> a.bar
<bound method foo.bar of <__main__.foo instance at 0x0076BCF8>>
>>> nothing
<function nothing at 0x007A8B20>
>>> foo.bar
<unbound method foo.bar>
>>> foo.printOne(foo(), 'hiya!')
printOne: hiya!
>>>

Searching the docs for bound, unbound, method and function turns up:

http://python.org/doc/current/lib/typesmethods.html


HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list