Attaching functions to objects as methods

tac-tics tactics40 at gmail.com
Fri Jul 7 19:29:38 EDT 2006


Python is a crazy language when it comes to object versatility. I know
I can do:

>>> class test:
...    def __init__(self):
...         pass
>>> x = test()
>>> def fun():
...     print "fun"
>>> x.fun = fun
>>> x.fun()
fun
>>>

However, experimenting shows that these attached functions are not
bound to the object. They do not accept the 'self' parameter. I want to
know how one goes about doing that. How do I bind a function to an
object to act as if it were a method? It looks like the classmethod()
built-in looks *similar* to what I want to do, but not quite.

Any help would be awesome =-)




More information about the Python-list mailing list