Referring to class methods in class attributes

Arnaud Delobelle arnodel at googlemail.com
Wed Feb 17 16:24:36 EST 2010


Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> writes:

[...]
> class Foo(object):
>     def bar(self):
>         return "baaz"
>
> print Foo.__dict__.keys()
> print type(Foo.__dict__['bar'])
>
>
> So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? The
> answer is : attribute lookup rules and the descriptor protocol.

It's true of Python 2.X.  In Python 3 there are no more unbound method:

Python 3.2a0 (py3k:75274, Oct  7 2009, 20:25:52) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...    def f(self): pass
... 
>>> A.f
<function f at 0x445b28>
>>> A.f is A.__dict__['f']
True

-- 
Arnaud



More information about the Python-list mailing list