Python doesn't know class of function ? : class C:def f():pass; l=[f]; print C.l[0].im_class

Terry Reedy tjreedy at udel.edu
Fri Mar 19 09:01:44 EST 2004


"Robert" <k.robert at gmx.de> wrote in message
news:19804fd8.0403190226.4a060ed0 at posting.google.com...

Functions do not have a class.

> Python doesn't know the class of a method when container not direct
> class attribute:
>
> >>> class X:
> ... def f():pass
> ... g=f

at this point, f and g are both *functions*

> ... l=[f]

and l is a list containing function f

> ...
> >>> X.g
> <unbound method X.f>

When you ask for attribute g and function g is found, g gets wrapped and
returned as an unbound method.

> >>> X.l[0]
> <function f at 0x01A9E1F0>

l is a list so no wrapping is done

> >>> X.l[0].im_class
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> AttributeError: 'function' object has no attribute 'im_class'

> why is l[0] a <function>. Any possibility to find the class of this
> beast dynamically?

To repeat, functions do not have a class.  Any function defined anywhere
can be made an attribute of multiple classes (that do not have attribute
addition turned off).

Terry J. Reedy







More information about the Python-list mailing list