How do I create an array of functions?

S

Steven W. Orr

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
 
D

Diez B. Roggisch

Steven said:
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
 
R

Rob Wolfe

Steven said:
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]]()
 
P

Paul Rubin

Steven W. Orr said:
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
 
S

Steven D'Aprano

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?
 
R

Rob Wolfe

Steven said:
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.
 
J

John Machin

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

Steven D'Aprano

# 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.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top