473,240 Members | 1,845 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,240 software developers and data experts.

mapping a string to an instancemethod

mh
The following bit of code will allow an instance member to
be called by reference. How can I map a string (e.g.
"hello1" or "Foo.hello1" to a the instance member?

class Foo:
def hello1(self, p):
print 'hello1', p
def hello2(self, p):
print 'hello2', p
def dispatch(self, func, p):
func(self,p)

f=Foo()
f.dispatch(Foo.hello1, 23)
f.dispatch(Foo.hello1, 24)

f.dispatch_as_string("hello1", 23) ## this is what I want to do.

Many TIA and apologies if this is a FAQ, I googled and couldn't
find the answer.

--
Mark Harrison
Pixar Animation Studios
Aug 1 '08 #1
1 1237
In article <7w******************@nlpi067.nbdc.sbc.com>, mh@pixar.com
wrote:
The following bit of code will allow an instance member to
be called by reference. How can I map a string (e.g.
"hello1" or "Foo.hello1" to a the instance member?

class Foo:
def hello1(self, p):
print 'hello1', p
def hello2(self, p):
print 'hello2', p
def dispatch(self, func, p):
func(self,p)

f=Foo()
f.dispatch(Foo.hello1, 23)
f.dispatch(Foo.hello1, 24)

f.dispatch_as_string("hello1", 23) ## this is what I want to do.
Do what's below. Then learn about *args to make a version that
works with variable numbers of parameters...

class Foo:
def hello1(self, p):
print 'hello1', p
def hello2(self, p):
print 'hello2', p
def dispatch(self, func, p):
func(self,p)
def dispatch_as_string(self, funcname, p):
getattr(self, funcname)(p)

f = Foo()
f.dispatch_as_string('hello1', 'world')
Many TIA and apologies if this is a FAQ, I googled and couldn't
find the answer.
--
David C. Ullrich
Aug 1 '08 #2

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

Similar topics

7
by: Rim | last post by:
Hi, It appears to me the simplest way to add a function to a class outside of the class declaration is as follows: >>> class a(object): .... pass .... >>> def f(self): .... print 'hi'
4
by: bonono | last post by:
I came across this while searching for a way to DIY partial(), until it is available in 2.5 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/229472 However, when trying for the...
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
8
by: dynamo | last post by:
i want to add '.' to another string .However the interpreter does not seem to see '.' as a string but something to do with the instancemethod.Does anyone know how to use a dot as a string.I have...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.