473,398 Members | 2,188 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,398 software developers and data experts.

How do I create an array of functions?

I have a table of integers and each time I look up a value from the table
I want to call a function using the table entry as an index into an array
whose values are the different functions. I haven't seen anything on how
to do this in python.

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Feb 19 '07 #1
7 33716
Steven W. Orr schrieb:
I have a table of integers and each time I look up a value from the
table I want to call a function using the table entry as an index into
an array whose values are the different functions. I haven't seen
anything on how to do this in python.
def f():
pass

fmap = { key: f }
fmap[key]()
Diez
Feb 19 '07 #2

Steven W. Orr wrote:
I have a table of integers and each time I look up a value from the table
I want to call a function using the table entry as an index into an array
whose values are the different functions. I haven't seen anything on how
to do this in python.
Do you mean something like that?

# test.py

def fun1(): return "fun1"
def fun2(): return "fun2"
def fun3(): return "fun3"

# list of functions
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]
tab = range(len(dsp))
print dsp[tab[2]]()

# dictionary of functions
d = dict([(fname, f) for fname, f in globals().items() if
callable(f)])
tab = [fname for fname, f in sorted(globals().items()) if callable(f)]
print d[tab[2]]()

--
HTH,
Rob

Feb 19 '07 #3
"Steven W. Orr" <st****@syslang.netwrites:
I have a table of integers and each time I look up a value from the
table I want to call a function using the table entry as an index into
an array whose values are the different functions. I haven't seen
anything on how to do this in python.
func_array = [f1, f2, f3] # array of functions
index = table_lookup()
func_array[index](x,y,z) # select a function and call it
Feb 19 '07 #4
On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:
>
Steven W. Orr wrote:
>I have a table of integers and each time I look up a value from the table
I want to call a function using the table entry as an index into an array
whose values are the different functions. I haven't seen anything on how
to do this in python.

Do you mean something like that?

# test.py

def fun1(): return "fun1"
def fun2(): return "fun2"
def fun3(): return "fun3"

# list of functions
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]
Hmmm... when I try that, I get dozens of other functions, not just fun1,
fun2 and fun3. And not just functions either; I also get classes.

Does Python have a function that will read my mind and only return the
objects I'm thinking of?

--
Steven.

Feb 19 '07 #5

Steven D'Aprano wrote:
On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:

Steven W. Orr wrote:
I have a table of integers and each time I look up a value from the table
I want to call a function using the table entry as an index into an array
whose values are the different functions. I haven't seen anything on how
to do this in python.
Do you mean something like that?

# test.py

def fun1(): return "fun1"
def fun2(): return "fun2"
def fun3(): return "fun3"

# list of functions
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]

Hmmm... when I try that, I get dozens of other functions, not just fun1,
fun2 and fun3. And not just functions either; I also get classes.
Oh, really? Where are these _other_ functions and classes
in *MY* example?
Does Python have a function that will read my mind and only return the
objects I'm thinking of?
Your sarcasm is unnecessary.
Using of `globals` function was easier to write this example.
That's all.

--
Rob

Feb 19 '07 #6
On Feb 19, 11:47 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
On Mon, 19 Feb 2007 00:16:39 -0800, Rob Wolfe wrote:
Steven W. Orr wrote:
I have a table of integers and each time I look up a value from the table
I want to call a function using the table entry as an index into an array
whose values are the different functions. I haven't seen anything on how
to do this in python.
Do you mean something like that?
# test.py
def fun1(): return "fun1"
def fun2(): return "fun2"
def fun3(): return "fun3"
# list of functions
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]

Hmmm... when I try that, I get dozens of other functions, not just fun1,
fun2 and fun3. And not just functions either; I also get classes.

Does Python have a function that will read my mind and only return the
objects I'm thinking of?
Yup.

Stevens_mind = r"fun[1-3]$"

After "if callable(f)", put

and re.match(Stevens_mind, fname)
and not isinstance(f, type)


Feb 19 '07 #7
On Mon, 19 Feb 2007 05:17:03 -0800, Rob Wolfe wrote:
# test.py

def fun1(): return "fun1"
def fun2(): return "fun2"
def fun3(): return "fun3"

# list of functions
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]

Hmmm... when I try that, I get dozens of other functions, not just fun1,
fun2 and fun3. And not just functions either; I also get classes.

Oh, really? Where are these _other_ functions and classes
in *MY* example?
I ran your example, word for word. Copied it and pasted it into my Python
session.
>Does Python have a function that will read my mind and only return the
objects I'm thinking of?

Your sarcasm is unnecessary.
Using of `globals` function was easier to write this example. That's all.
Actually, it wasn't easier to write at all.

Your version:
dsp = [f for fname, f in sorted(globals().items()) if callable(f)]

Sensible version:
dsp = [fun1, fun2, fun3]

Not only is your version brittle, but it is also about three times as
much typing.

--
Steven.

Feb 19 '07 #8

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

Similar topics

9
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
by: Stou Sandalski | last post by:
Hi, I have a python library created by wrapping the C++ library using Boost.Python, the problem is that the wrappers are not very pythonic.... so I want to add some methods that do not exist in...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
6
by: The Natural Philosopher | last post by:
I am trying to create what amounts to an array of 'structures'. I.e. I want to dynamically add to and access an object as myobject.anothernumber // actually represents a nesting level. and ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.