Re: Building a function call?



On Wed, 13 Jul 2005 06:16:54 -0700, Robert Kern wrote:

> Duncan Booth wrote:
>> Francois De Serres wrote:
>>
>>>Having a string: "dothat"
>>>and a tuple: (x, y)
>>>1. What's the best way to build a function call like: dothat(x,y)?
[snip]
>> No, none of this is a good place to use eval.
[snip]
>> import otherModule
>> vars(otherModule)[aString](*aTuple)
>
> Ick! Please:
> getattr(otherModule, aString)(*aTuple)


Or, remember that functions are first class objects in Python. Instead of
passing around the function name as a string, pass around a reference to
the function itself. Something like this:


def dothis(x,y):
return x-y

def dothat(x,y):
return x+y

somefunction = dothis
somefunction(3, 2)
=> returns 1

somefunction = dothat
somefunction(3, 2)
=> returns 5

allfunctions = [dothis, dothat]
for func in allfunctions:
print func(3, 2)
=> prints 1 then 5

If you want to collect user-supplied strings and use them to find a
function, you could use eval (terribly risky and unsafe), or you could do
something like this:

funcnames = {}
for func in allfunctions:
funcnames[func.__name__] = func
F = raw_input("Enter the name of a function: ")
try:
funcnames[F](3, 2)
except KeyError:
print "Function '%s' not found!" % F


In my humble opinion, people muck about with eval, locals and globals far
too often. It is unclear, hard to maintain, and frequently a security
risk. These confusing, unsafe practices can usually be avoided by
remembering that functions are first class objects just like ints, strings
and lists.



--
Steven.


.



Relevant Pages

  • Re: script to copy user profile!!
    ... Also, consider using XCopy: ... Specifies a list of files containing strings. ...
    (microsoft.public.windows.server.scripting)
  • Re: chkrootkit reporting sshd vulnerable?
    ... >>ha, me, an expert?, ha ha) and it gave the strings from sshd but after ... > Some of chkrootkit works by running the 'strings' command on certain programs ...
    (comp.security.ssh)
  • Re: BWT, tuning for speed
    ... Suppose you have sorted all strings starting with 'a'. ... (Note that sorting on strings strictly is O(M * N log N) ... <END SNIP> ... tandem repeats and embedded tandem repeats with direct comparison ...
    (comp.compression)
  • Re: String Manipulation
    ... This is a simple parser for the strings provided. ... our @vars; ... identifier identifier array(?) ';' { ...
    (perl.beginners)
  • Re: Boost process and C
    ... snip ... ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... Handling an RTF document that you will be writing to a variable length record in a database. ...
    (comp.lang.c)