470,826 Members | 2,244 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

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 31598
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Steve | last post: by
5 posts views Thread by Lance | last post: by
reply views Thread by Stou Sandalski | last post: by
6 posts views Thread by The Natural Philosopher | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.