473,385 Members | 1,292 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.

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 1242
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.