swisscheese wrote: > I'm trying to write a function that takes an arbitrary object and > method. > The function applies the method to the object (and some other stuff). > I get error "Test instance has no attribute 'method' " > How can I make this work? > > def ObjApply (object,method): > object.method () > > class Test: > def test1 (self): print "Hello" > def test2 (self): > ObjApply (self,self.test1) > > ta = Test () > ta.test2 () > Your code appears to be based on a slight misunderstanding of method calls. In your Test class you are calling ObjApply with two arguments, the second of which is a "bound method" (in other words, it already implicitly references the instance of which it is a method).
In still other words, there is really no need for two arguments to ObjApply! Perhaps you need to think through your goals more clearly ... >>> def ObjApply(method): ... method() ... >>> class Test: ... def test1(self): print "Hello from", self ... def test2(self): ObjApply(self.test1) ... >>> ts = Test() >>> ts.test2() Hello from <__main__.Test instance at 0x18cd238c> >>> So, what is it you *really* want to do? regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd www.holdenweb.com Love me, love my blog holdenweb.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list