Call variable as a function
Steve Holden
steve at holdenweb.com
Thu Mar 3 17:22:51 EST 2005
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
>> >>>
>> Hope this helps.
>
>
> Thanks man it does help.
>
Then it might help even more to realize that you can use lists and
dictionaries of functions as well:
>>> def f1(x):
... return x*2
...
>>> def f2(x):
... return "This is an %s" % x
...
>>> def f3(x):
... return "%s:%s" % (x, x)
...
>>> fl = [f1, f2, f3]
>>> for s in ("bigstring", "tiny"):
... for i in range(len(fl)):
... print fl[i](s)
...
bigstringbigstring
This is an bigstring
bigstring:bigstring
tinytiny
This is an tiny
tiny:tiny
>>>
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
More information about the Python-list
mailing list