Re: Call variable as a function

2005-03-04 Thread Nick Coghlan
Steve Holden wrote: There's always a way: >>> def test(): ... print "hello" ... >>> var = "test" >>> eval("%s()" % var) hello I'd go with locals() for the simple case of a name lookup: Py> def test(): ... print "Hi there!" ... Py> var = "test" Py> locals()[var]() Hi there! Cheers, Nick. -- N

Re: Call variable as a function

2005-03-03 Thread Steve Holden
Chmouel Boudjnah wrote: Steve Holden wrote: But it depends how you are creating the reference to the function. The above is required if all you have is a string, but it would also be possible to set the variable to the function rather than the function's name >>> var = test >>> var() hello >>

Re: Call variable as a function

2005-03-03 Thread Steve Holden
Chmouel Boudjnah wrote: Hey, It's look simple for most of the people here but i can't figure out to that. If i have : def test(): print "foo" and: var = "test" how do i call function test from var, kind of ${$var} in some others languages (or eval()) There's always a way: >>> def test(): ..

Call variable as a function

2005-03-03 Thread Chmouel Boudjnah
Hey, It's look simple for most of the people here but i can't figure out to that. If i have : def test(): print "foo" and: var = "test" how do i call function test from var, kind of ${$var} in some others languages (or eval()) Cheers, Chmouel. -- http://mail.python.org/mailman/listinfo/py