apply for objects

Sander Hahn shahn at cs.vu.nl
Thu Jun 22 03:52:18 EDT 2000


Hello Michal!

Thank you for your reply!

I wanted to call a method of an object in Python.
The way to call a function is:

>>> def f(x, y):
	print str(x) + str(y)
	
>>> apply(f, ('hello',), {'y': 6})
hello6

That works, however when you use apply on a method you get the following
error:

>>> class C:
	def m(x, y):
		print str(x) + str(y)
		
		
>>> c = C()

>>> apply(c.m, ('hello', ' bye now'), {})
Traceback (innermost last):
  File "<pyshell#12>", line 1, in ?
    apply(c.m, ('hello', ' bye now'), {})
TypeError: too many arguments; expected 2, got 3

I previously got errors about no __call__ or something weird...!?!
However i found out that you must use the im_func from the method to get
the corresponding function:

>>> apply(c.m.im_func, ('hello', ' bye now'), {})
hello bye now

So, my problem is solved for now :-)

Thank you for your friendly reply!

Regards,
Sander Hahn





More information about the Python-list mailing list