Adding functions to classes after definition
Gabriel Genellina
gagsl-py at yahoo.com.ar
Mon Jan 8 21:03:45 EST 2007
At Monday 8/1/2007 21:47, Gerard Brunick wrote:
>Consider: A)
> >>> class C(object):
>... pass
>...
> >>> def f(*args):
>... print args
>...
> >>> C.f = f
> >>> C.f
><unbound method C.f>
> >>> c=C()
> >>> c.f()
>(<__main__.C object at 0x04A51170>,)
>
>And B)
>
> >>> del c
> >>> C.f = types.MethodType(f, None, C)
> >>> C.f
><unbound method C.f>
> >>> c = C()
> >>> c.f()
>(<__main__.C object at 0x04A51290>,)
>
>I don't understand A). It is my vague understanding, that methods are
>really properties that handle binding on attribute access, so B) should
>be the "right" way to add a method to a class after definition.
This is implemented using descriptors. A function object has a __get__ method:
py> f.__get__
<method-wrapper object at 0x00BC8990>
py> C.__dict__['f'] is f
True
py> C.f is f
False
py> C.f.im_func is f
True
So, when you assign C.f=f, nothing special happens; but when the
function is retrieved from the class, the __get__ method is invoked,
returning an "unbound method".
> Why does
>A show up as a method? Shouldn't it still just be a function? Certainly
>when you define a class, there is some magic in the __new__ method that
>turns functions in the initial dictionary into methods, but does this still
>happen for all setattr after that?
There's nothing special in __new__ (relating to this, at least). Nor
even when setting the attribute; the magic happens when you *get* the
method as an attribute of the class object. Functions don't even have
a __set__:
py> f.__set__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'function' object has no attribute '__set__'
>Is is possible to set a class attribute
>equal to a regular (types.FunctionType) function?
Yes, that's what actually happens. It's not easy to *retrieve* it
later without getting a MethodType.
>Any references that discuss these issues would be greatly appreciated.
Descriptors are documented somewhere... I think they came in Python
2.2 along with new-style classes.
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list