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

Robert Brewer fumanchu at amor.org
Fri Apr 22 17:58:21 EDT 2005


mehmetmutigozel at gmail.com wrote:
> I was thinking about something like the following;
> 
> >>> a=[ t**n for n in range(4) ]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> NameError: name 't' is not defined
> >>>
> 
> or
> 
> >>> a=[ lambda t: t**n for n in range(4) ]
> >>> t=2
> >>> a
> [<function <lambda> at 0x403dcc6c>, <function <lambda> at 0x403dcca4>,
> <function <lambda> at 0x403dccdc>, <function <lambda> at 0x403dcd14>]
> >>> t=3
> >>> a
> [<function <lambda> at 0x403dcc6c>, <function <lambda> at 0x403dcca4>,
> <function <lambda> at 0x403dccdc>, <function <lambda> at 0x403dcd14>]
> >>>
> 
> is something like that possible? Will you give me advice about that?

Are you looking for this?

>>> def four_powers(base):
... 	return [base ** exp for exp in xrange(4)]
... 
>>> four_powers(2)
[1, 2, 4, 8]
>>> four_powers(3)
[1, 3, 9, 27]

Function-calling isn't deprecated yet, to my knowledge... ;)


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list