Converting a string to a function pointer

  • Thread starter =?ISO-8859-1?Q?H=E5kan_Persson?=
  • Start date
?

=?ISO-8859-1?Q?H=E5kan_Persson?=

Hi.

I am trying to "convert" a string into a function pointer.
Suppose I have the following:

from a import a
from b import b
from c import c

funcString = GetFunctionAsString()

and funcString is a string that contains either "a", "b" or "c".
How can I simply call the correct function?
I have tried using getattr() but I don't know what the first (object)
argument should be in this case.

Thanks,
Håkan Persson
 
J

John Machin

Hi.

I am trying to "convert" a string into a function pointer.
Suppose I have the following:

from a import a
from b import b
from c import c

funcString = GetFunctionAsString()

and funcString is a string that contains either "a", "b" or "c".
How can I simply call the correct function?
I have tried using getattr() but I don't know what the first (object)
argument should be in this case.

Try this:
from sys import exit
globals()
'__name__': said:
afunc = globals()["exit"]

Do you really need to use the "from X import Y" style? Consider the
following alternative:
 
S

Steve Holden

Håkan Persson said:
Hi.

I am trying to "convert" a string into a function pointer.
Suppose I have the following:

from a import a
from b import b
from c import c

funcString = GetFunctionAsString()

and funcString is a string that contains either "a", "b" or "c".
How can I simply call the correct function?
I have tried using getattr() but I don't know what the first (object)
argument should be in this case.

Thanks,
Håkan Persson
Well, one really horrible way would be

getattr(sys.modules['__main__'], funcString)

and, of course, if you were going to use it a lot you could optimize
slightly with

this = sys.modules['__main__']

followed later by

this = getattr(this, funcString)

Overall, however, it might be better to set uip a mapping for the
functions you need to be accessible. This has the further merits that

a) You can heve different, and/or multiple names for a function
b) You can trap errors more easily
c) erm, I forgot the third advantage, consider this the Spanish
Inquisition in reverse :)

So:

funcMap = {
"a": a,
"A": a,
"b": b,
"exoticName": c }

...

funcString = GetFunctionAsString()
try:
f = funcMap(funcString)
except KeyError:
print "No such function"
raise SomethingElse
result = f(args)

regards
Steve
 
T

Tim Roberts

Håkan Persson said:
I am trying to "convert" a string into a function pointer.
Suppose I have the following:

from a import a
from b import b
from c import c

funcString = GetFunctionAsString()

and funcString is a string that contains either "a", "b" or "c".
How can I simply call the correct function?
I have tried using getattr() but I don't know what the first (object)
argument should be in this case.

You've received a lot of hi-tech answers, but don't overlook the simple
(and possibly more secure) answer:

lookup = {
'a': a,
'b': b,
'c': c,
}

if lookup.has_key(myFunction):
return lookup[myFunction]()
else:
print "Couldn't find", myFunction
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top