473,668 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33816
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.TH IS.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(Steven s_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
2520
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
338
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 looking for something like this: array = {func1(a, b, c), func2(b, c), func3(a), func4(a,b,c,d)....} If I need to call a function I want to be able to call the function by writing something like array Is that or anything close to that format...
5
7367
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 after a while). Can somebody please tell me what I'm doing wrong? Here is a sample: Public Shared Function CreateBitmapFromArray( _ ByVal width As Integer, _ ByVal height As Integer, _ ByVal pixelFormat As Drawing.Imaging.PixelFormat, _
5
2131
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 recognize that the created array is the same as the declared interface argument. The C# function is expecting to receive a 2 dimensioned array and is declared like this: public object InteropFunc( string s1, object arValues ); What should the ASP...
23
7397
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
0
1090
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 the C+ + implementation, that would create a better Python interface. For example to initialize the data in an object in the library one must iterate through every point, setting a value for each individually. That's the way it works in C++...
10
3969
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 in the list A i want to associate an undetermined number of items. The complication for me is the fact that the items to associate in list A are undetermined.
3
4839
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 pointers (delegates). I assume I need to use the array< T keyword to allocate an array of delegates, and then initialize the array by setting each array element to the pointers (handles) of the functions I'll be invoking. I've been trying to...
6
1718
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 myobject.elementintheDOM // this is a pointer to //store particular elements..i.e.
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7398
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.