[Python-ideas] Functions and Unbound methods

MRAB python at mrabarnett.plus.com
Thu Feb 21 04:50:21 CET 2013


On 2013-02-21 02:11, robert2682 wrote:
>
>
> Hi,
>
> I'm new; greetings all!
>
> I'm not sure if this is a bug or feature, but it confused me so I thought
> I'd raise the issue.
>
>
> class a:
>     def b (self):
>       pass
>
>     foo = ('Hello', b)
>
> class c(a):
>     def d(self):
>       t = type (self. __class__. foo [1])
>       print t
>       t = type (self. __class__. b)
>       print t
>
> e = c ()
> e. d()
>
> prints <type 'function'> for the first print,
> and it seems to me it should be an instancemethod
>
[snip]
I think what's happening is that it's defining 'b' as a function in the
class's namespace, storing a reference to that function in the tuple,
and then, when the class definition ends, it's wrapping the function as
a method.

You'll find:

 >>> a.foo[1]
<function b at 0x00B42C30>
 >>> a.b
<unbound method a.b>
 >>> a.b.im_func is a.foo[1]
True




More information about the Python-ideas mailing list