Passing a method indirectly

S

swisscheese

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 ()
 
?

=?iso-8859-1?B?QW5kcuk=?=

swisscheese said:
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 ()

You need to add one line (2nd one below).

def ObjApply (object, method):
object.method = method
object.method()

class Test:
def test1 (self): print "Hello"
def test2 (self):
ObjApply (self, self.test1)

ta = Test ()
ta.test2 ()

André
 
F

Farshid Lashkari

You don't need to pass the object along with the method. The method is
bound to the object. Simply call the method by itself:

def ObjApply(method):
method()

class Test:
def test1 (self): print "Hello"
def test2 (self):
ObjApply(self.test1)

ta = Test ()
ta.test2 ()

If you wanted to pass an unbound method, then it would look like the
following:

def ObjApply(object,method):
method(object)

class Test:
def test1 (self): print "Hello"
def test2 (self):
ObjApply(self,Test.test1)

ta = Test ()
ta.test2 ()
 
S

Steve Holden

swisscheese said:
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 ...
... method()
... ... def test1(self): print "Hello from", self
... def test2(self): ObjApply(self.test1)
...
So, what is it you *really* want to do?

regards
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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top