Uwe Mayer wrote:
Unfortunately I want to assign a handler function to an object and something
like this does not work:

class Foobar(object): pass

...

a = Foobar()
def a.handler():

File "<stdin>", line 1 def a.handler(): ^ SyntaxError: invalid syntax

But this does work, or something close to it:

>>> class Foobar(object): pass
...
>>> def handler(self):
...   print 'in handler'
...
>>> f = Foobar()
>>> f.handler()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'Foobar' object has no attribute 'handler'
>>>
>>> import new
>>> f.handler = new.instancemethod(handler, f)
>>> f.handler()
in handler


-Peter -- http://mail.python.org/mailman/listinfo/python-list

Reply via email to