473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing a method indirectly

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

Mar 5 '06 #1
4 1165

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


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é

Mar 5 '06 #2
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 ()
Mar 5 '06 #3
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

Mar 5 '06 #4
Thanks - that worked!
Thanks to the other replies also.

Mar 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
5
by: Siv | last post by:
Hi, I have a class module that I have created and I have looked at the various descriptions of how you should implement a dispose method in your own class and I am finding it mighty confusing. ...
5
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.