a=[ lambda t: t**n for n in range(4) ]

Terry Hancock hancock at anansispaceworks.com
Fri Apr 22 19:25:53 EDT 2005


On Friday 22 April 2005 05:18 pm, mehmetmutigozel at gmail.com wrote:
> Thanx for your replies.
> 
> I'm looking for array of functions.
> Something like a=[ sin(x) , cos(x) ]

You had a list of lambda functions in your first post and in the
subject line still. How is that not what you wanted?

If you want an *answer*, you need to ask a *question*. 

Perhaps you don't know how to call such functions?  E.g.:

a=[ lambda t: t**n for n in range(4) ]

>>> a[2](3)
27

If you want to see *names* for the functions, you have two
choices: either used named functions, 

def unity(t): return 1
def identity(t): return t
def square(t): return t**2
def cube(t): return t**3
a = [unity, identity, square, cube]

>>> a
[<function unity at 0x401e609c>, <function identity at 0x401e6b54>,
<function square at 0x401e6b8c>, <function cube at 0x401e6ca4>]

or replace the list with a dictionary, e.g.:

a = dict([('t**%d' % n, lambda t: t**n) for n in range(4)])

>>> a
{'t**0': <function <lambda> at 0x401e6bc4>, 't**1': <function <lambda> at 0x401e6bfc>, 
't**2': <function <lambda> at 0x401e6c34>, 't**3': <function <lambda> at 0x401e6c6c>}
>>> a.keys()
['t**0', 't**1', 't**2', 't**3']
>>> a['t**3'](4)
64



--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list