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

Michael Spencer mahs at telcopartners.com
Fri Apr 22 17:54:32 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

Perhaps you mean:
  >>> a = lambda t: [t**n for n in range(4)]
  >>> a(2)
  [1, 2, 4, 8]
  >>>

but that is better written as:
  >>> def a(t):
  ...     return [t**n for n in range(4)]
  ...
  >>>
  >>> a(2)
  [1, 2, 4, 8]
  >>>

Michael







More information about the Python-list mailing list