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:
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
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.
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
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
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
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
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
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
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
10 matches
Mail list logo