Re: programmatically calling a function

2005-03-07 Thread Larry Bates
I see lots of others have made suggestions, but here is a method that I use frequently: define a dictionary that contains references to your functions: def foo(): . . whatever it does . def bar(): . . whatever it does . xfer={'foo', foo, 'bar', bar} Then you can write for fname in fnames:

Re: programmatically calling a function

2005-03-05 Thread Carl Banks
Carl Banks wrote: > Doug Schwarz wrote: > > I don't see how getattr solves the original problem. What, exactly, > is > > the first argument to getattr? > > > mod = __import__(__this__) That should be __import__(__name__) Silly me. -- CARL BANKS -- http://mail.python.org/mailman/listinfo/py

Re: programmatically calling a function

2005-03-05 Thread Carl Banks
Doug Schwarz wrote: > I don't see how getattr solves the original problem. What, exactly, is > the first argument to getattr? mod = __import__(__this__) f = getattr(mod,"foo") I tend to prefer this over globals() because it seems a little less magical to me, especially when setting a global.

Re: programmatically calling a function

2005-03-05 Thread Reinhold Birkenfeld
Doug Schwarz wrote: >> > Dave, >> > >> > I think eval might be what you're looking for: >> > >> > f = eval('len') >> > length = f([1,2,3]) >> >> But only if the string given to eval is checked thorougly for allowed >> contents. Better use getattr. > > Actually, upon reading Peter Hansen's repl

Re: programmatically calling a function

2005-03-05 Thread Doug Schwarz
In article <[EMAIL PROTECTED]>, Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: > Doug Schwarz wrote: > > > Dave, > > > > I think eval might be what you're looking for: > > > > f = eval('len') > > length = f([1,2,3]) > > But only if the string given to eval is checked thorougly for allowed > c

Re: programmatically calling a function

2005-03-05 Thread Reinhold Birkenfeld
Doug Schwarz wrote: > Dave, > > I think eval might be what you're looking for: > > f = eval('len') > length = f([1,2,3]) But only if the string given to eval is checked thorougly for allowed contents. Better use getattr. Reinhold -- http://mail.python.org/mailman/listinfo/python-list

Re: programmatically calling a function

2005-03-05 Thread Doug Schwarz
In article <[EMAIL PROTECTED]>, Dave Ekhaus <[EMAIL PROTECTED]> wrote: > hi > > i'd like to call a python function programmatically - when all i have > is the functions name as a string. i.e. > > > fnames = ['foo', 'bar'] > > for func in fnames: > > # > # how do i c

Re: programmatically calling a function

2005-03-05 Thread Simon Percivall
You might also want to take a peek at the getattr() function: http://docs.python.org/lib/built-in-funcs.html#l2h-31 -- http://mail.python.org/mailman/listinfo/python-list

Re: programmatically calling a function

2005-03-04 Thread Peter Hansen
Dave Ekhaus wrote: i'd like to call a python function programmatically - when all i have is the functions name as a string. i.e. > fnames = ['foo', 'bar'] The usual answer at this point is to say that functions are "first class objects" in Python, which basically means you can haul around ref

programmatically calling a function

2005-03-04 Thread Dave Ekhaus
hi i'd like to call a python function programmatically - when all i have is the functions name as a string. i.e. fnames = ['foo', 'bar'] for func in fnames: # # how do i call function 'func' when all i have is the name of the function ??? # def foo(): print 'foo