importing a method

Flavio fccoelho at gmail.com
Mon Nov 28 12:44:00 CET 2005


If you read my original post, I had no intention of atributing the
user's method to the class, but to the instance.

Anyway I figure it out myself, and its quite a Pythonic solution:
>>> class Foo:
            name='John'

>>> a=Foo()

>>> def p(parent):
            self=parent
            print 'Hi, %s!'%self.name


>>> a.met=p

>>> a.met(a)
Hi, John!

This works the same way an object's built-in method would work, since
all methods receive a reference to the parent object through the
required argument "self".

class Foo:
   def met(self):
       print self

Thanks for all the replies, they helped to catalize my own thoughts!

Flávio




More information about the Python-list mailing list