Attaching functions to objects as methods

T

tac-tics

Python is a crazy language when it comes to object versatility. I know
I can do:
.... def __init__(self):
.... pass
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 =-)
 
S

Steven Bethard

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

... def __init__(self):
... pass
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?

Functions are descriptors[1], and their __get__ method is used to bind
them to a particular instance::
... def __init__(self):
... pass
... ... print "fun", self
... fun <__main__.test object at 0x00E6D730>

Note that calling __get__ returns a bound method, and that method can be
bound to an instance or a class depending on the parameters of __get__::
<unbound method test.fun>



[1] http://users.rcn.com/python/download/Descriptor.htm

STeVe
 
J

John Machin

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

... def __init__(self):
... pass
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 =-)

Call me crazy, but wasn't this discussed in a thread only a few days ago?

Injecting a method into a class, so that even already-created instances
have that method, is trivial:
.... pass
........ print "fun"
.... self.frob = arg
....
Injecting a "private" method into a particular instance is not much more
complicated:
.... print "own"
.... self.ozz = arg
....Traceback (most recent call last):

and no __makeyoureyesbleed__ __doubleunderscoremessingabout__ required :)

Cheers,
John
 
J

John Machin

Experimenting, I found that


works as well. Any comments on this way?

Appears to work. Less typing for a no-argument method, but you need to
specify the argument list *twice* compared with *zero* times with the
types.MethodType way of doing it.
 
S

Steven Bethard

John said:
Injecting a "private" method into a particular instance is not much more
complicated:

... print "own"
... self.ozz = arg
...
Traceback (most recent call last):


and no __makeyoureyesbleed__ __doubleunderscoremessingabout__ required :)

=)

But of course you have to import a module instead. I'm not a huge fan of
the types module because it's generally unnecessary, and I'm not
particularly afraid of double-underscores as I write a lot of __init__
methods. ;-)

To the OP: regardless of which solution you eventually go with, it's
definitely worth understanding how descriptors_ work. New-style classes
wouldn't work without them.

... _descriptors: http://users.rcn.com/python/download/Descriptor.htm

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top